Java System.currentTimeMillis() equivalent in C#

C#Java.NetDatetime

C# Problem Overview


What is the equivalent of Java's System.currentTimeMillis() in C#?

C# Solutions


Solution 1 - C#

An alternative:

private static readonly DateTime Jan1st1970 = new DateTime
    (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static long CurrentTimeMillis()
{
    return (long) (DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}

Solution 2 - C#

A common idiom in Java is to use the currentTimeMillis() for timing or scheduling purposes, where you're not interested in the actual milliseconds since 1970, but instead calculate some relative value and compare later invocations of currentTimeMillis() to that value.

If that's what you're looking for, the C# equivalent is Environment.TickCount.

Solution 3 - C#

If you are interested in TIMING, add a reference to System.Diagnostics and use a Stopwatch.

For example:

var sw = Stopwatch.StartNew();
...
var elapsedStage1 = sw.ElapsedMilliseconds;
...
var elapsedStage2 = sw.ElapsedMilliseconds;
...
sw.Stop();

Solution 4 - C#

DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()

Solution 5 - C#

the System.currentTimeMillis() in java returns the current time in milliseconds from 1/1/1970

c# that would be

public static double GetCurrentMilli()
    {
        DateTime Jan1970 = new DateTime(1970, 1, 1, 0, 0,0,DateTimeKind.Utc);
        TimeSpan javaSpan = DateTime.UtcNow - Jan1970;
        return javaSpan.TotalMilliseconds;
    }

edit: made it utc as suggested :)

Solution 6 - C#

We could also get a little fancy and do it as an extension method, so that it hangs off the DateTime class:

public static class DateTimeExtensions
{
    private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    public static long currentTimeMillis(this DateTime d)
    {
        return (long) ((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
    }
}

Solution 7 - C#

The framework doesn't include the old seconds (or milliseconds) since 1970. The closest you get is DateTime.Ticks which is the number of 100-nanoseconds since january 1st 0001.

Solution 8 - C#

Here is a simple way to approximate the Unix timestamp. Using UTC is closer to the unix concept, and you need to covert from double to long.

TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
long millis = (long)ts.TotalMilliseconds;
Console.WriteLine("millis={0}", millis);

prints:

millis=1226674125796

Solution 9 - C#

I just consider the most straight forward way how to achieve what you've been striving for as follows:

DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond

Solution 10 - C#

If you want a timestamp to be compared between different processes, different languages (Java, C, C#), under GNU/Linux and Windows (Seven at least):

C#:

private static long nanoTime() {
   long nano = 10000L * Stopwatch.GetTimestamp();
   nano /= TimeSpan.TicksPerMillisecond;
   nano *= 100L;
   return nano;
}

Java:

java.lang.System.nanoTime();

C GNU/Linux:

static int64_t hpms_nano() {
   struct timespec t;
   clock_gettime( CLOCK_MONOTONIC, &t );
   int64_t nano = t.tv_sec;
   nano *= 1000;
   nano *= 1000;
   nano *= 1000;
   nano += t.tv_nsec;
   return nano;
}

C Windows:

static int64_t hpms_nano() {
   static LARGE_INTEGER ticksPerSecond;
   if( ticksPerSecond.QuadPart == 0 ) {
      QueryPerformanceFrequency( &ticksPerSecond );
   }
   LARGE_INTEGER ticks;
   QueryPerformanceCounter( &ticks );
   uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
   nano *= 100UL;
   return nano;
}

Solution 11 - C#

I know question asks for equivalent but since I use those 2 for the same tasks I throw in GetTickCount. I might be nostalgic but System.currentTimeMillis() and GetTickCount() are the only ones I use for getting ticks.

[DllImport("kernel32.dll")]
static extern uint GetTickCount();

// call
uint ticks = GetTickCount();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyashView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#BarendView Answer on Stackoverflow
Solution 3 - C#bstabileView Answer on Stackoverflow
Solution 4 - C#RamunasView Answer on Stackoverflow
Solution 5 - C#HathView Answer on Stackoverflow
Solution 6 - C#Joel CoehoornView Answer on Stackoverflow
Solution 7 - C#Cristian LibardoView Answer on Stackoverflow
Solution 8 - C#gimelView Answer on Stackoverflow
Solution 9 - C#TechCrapView Answer on Stackoverflow
Solution 10 - C#AubinView Answer on Stackoverflow
Solution 11 - C#BitterblueView Answer on Stackoverflow