Java System.currentTimeMillis() equivalent in C#

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

Asked by: Freddie321 | Posted: 28-01-2022

Answer 1

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;

Answered by: Rubie471 | Posted: 01-03-2022

Answer 2

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.

Answered by: Kirsten219 | Posted: 01-03-2022

Answer 3

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;

Answered by: First Name591 | Posted: 01-03-2022

Answer 4


Answered by: Dominik460 | Posted: 01-03-2022

Answer 5

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 :)

Answered by: Justin625 | Posted: 01-03-2022

Answer 6

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);

Answered by: Lana473 | Posted: 01-03-2022

Answer 7

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.

Answered by: Kelvin765 | Posted: 01-03-2022

Answer 8

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);



Answered by: Carlos351 | Posted: 01-03-2022

Answer 9

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

DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond

Answered by: Edgar564 | Posted: 01-03-2022

Answer 10

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


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



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 );
   QueryPerformanceCounter( &ticks );
   uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
   nano *= 100UL;
   return nano;

Answered by: Kelsey292 | Posted: 01-03-2022

Answer 11

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.

static extern uint GetTickCount();

// call
uint ticks = GetTickCount();

Answered by: Dainton309 | Posted: 01-03-2022

Similar questions

java - What is the equivalent of System.currenttimemillis() in Tcl

What is the equivalent of Java's System.currenttimemillis(); in Tcl ?

java - System.currentTimeMillis() for a given date?

This question already has answers here:

Is System.currentTimeMillis() the best measure of time performance in Java?

Is System.currentTimeMillis() the best measure of time performance in Java? Are there any gotcha's when using this to compare the time before action is taken to the time after the action is taken? Is there a better alternative?

JAVA's System.currentTimeMillis() or C#'s Environment.TickCount?

Hey, as of lately, I've been trying to find good ways to smoothen out thread sleeps (incase it gets disturbed or if your computer laggs etc). So which is an overall "better performance" method? JAVA's System.currentTimeMillis() method for C#: public static double GetCurrentMilliseconds() { DateTime staticDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); TimeSpan timeSpan = ...

java - Does the method System.currentTimeMillis() really return the current time?

Based on ideas presented in link I implemented several different "sleep methods". One of this methods was the "binary sleep", which looks like this: while (System.currentTimeMillis() < nextTimeStamp) { sleepTime -= (sleepTime / 2); sleep(sleepTime); } Because the check if the next time step is already...

java - When will System.currentTimeMillis() overflow?

I have a web app which orders stuff using a timestamp, which is just a long. My web app backend happens to be written in java, so I am using: long timestamp = System.currentTimeMillis(); what year (approximately) will this fail? I mean at some point, the range of a long is going to overflow, right? We may all be long-dead, but I'm just curious. Will it be like y2k all over again? What can I ...

java - Repeatedly Calling System.currentTimeMillis()/nanoTime()

I'm trying create a Thread that keeps track of the time elapsed if a certain event has occurred/is happening. I want to be able to use the elapsed time outside of the thread, like: if(theChecker.elapsedTime > 5) doThis(); The code that I have basically looks like this: public class Checker extends Thread { public int startTime, elapsedTime; @Override pu...

Java: What is the unit of the difference of two System.currentTimeMillis?

What is the unit of the difference of two System.currentTimeMillis ? start = System.currentTimeMillis(); longoperation(); elapsedTime = System.currentTimeMillis() - start; What is the unit of elapsed time here. It doesn't look like milliseconds. Is the above code segment the right way to find the time taken to execute longoperation()?

java - SQLite3 and System.currentTimeMillis()

I want to generate exact same values using SQLite strftime() or any other function as that of System.currentTimeMillis() function in java. Is there a possible way ? SELECT strftime('%s','now'); generates the current time but does not consider milliseconds but seconds. I want precision to miliseconds. EDIT based on Axarydax answer SELEC...

java - Safe to use System.currentTimeMillis() to generate a unique database ID?

I'm using System.currentTimeMillis() (which returns a long integer) in Java to generate a unique ID for database entities since I assume that it's not possible for these times to overlap at any point. Is this a safe assumption? For example, at the moment I get this: 1296691225227

java - "blinking" boolean with system.currentTimeMillis()

Why is this value always true? I just can't figure out how to have a boolean that "blinks" every second. long millis = System.currentTimeMillis(); boolean blink = (Math.floor(millis/1000 + 0.5)==Math.floor(millis/1000));

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)