C# version of Javascript Date.getTime()

What is the best way in c# to get the same result of javascript date.gettime() call?

The getTime() method returns the number of milliseconds since midnight of January 1, 1970 and the specified date.

You can use this solution:

private int GetTime()
{
   var time = (DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1));
   return (int)(time.TotalMilliseconds + 0.5);
}

 

Since JavaScript time is with respect to UTC, I think you will need something like this:

var st = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var t  = (DateTime.Now.ToUniversalTime() - st);
// t.TotalMilliseconds

Now you can use the TotalMilliseconds property of the Timespan.

The correct implementation (assuming the current time) is as follows:

DateTime utcNow = DateTime.UtcNow;
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long ts = (long)((utcNow - epoch).TotalMilliseconds);

The Java and JavaScript Date.getTime() methods return the number of milliseconds since 1 Jan 1970 00:00:00 GMT.

Since .NET
represents dates in Ticks (1 Tick = 0.1 nanoseconds or 0.0001 milliseconds) since 1 Jan 0001 00:00:00 GMT, we must use a
conversion formula where 621355968000000000 is the offset between the base dates in Ticks and 10000 the number of Ticks per
Millisecond.

Ticks = (MilliSeconds * 10000) + 621355968000000000
MilliSeconds = (Ticks - 621355968000000000) / 10000

I guess this will do the trick 🙂

public double MilliTimeStamp(DateTime TheDate)
        {
            DateTime d1 = new DateTime(1970, 1, 1);
            DateTime d2 = TheDate.ToUniversalTime();
            TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);

            return ts.TotalMilliseconds;
        }

(DateTime.Now - new DateTime (1970, 1, 1)).TotalMilliseconds

The currently accepted answer returns an int which is incorrect. It has to be Int64 or long. This is just rewriting the correct answer provided by Matt Johnson-Pint (and edited by Adaptabi) as one line. Please accept Matt Johnson-Pint’s answer. I checked it against actual javascript new Date().getTime() in the console to verify it returns the same number.

long JavascriptGetTime()
{
    return (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;            
}

Here is an extension method based off Enigma State’s answer

public static Int64 GetJavascriptTimeStamp(this DateTime dt)
{
    var nineteenseventy = new DateTime(1970, 1, 1);
    var timeElapsed = (dt.ToUniversalTime() - nineteenseventy);
    return (Int64)(timeElapsed.TotalMilliseconds + 0.5);
}

To use it for the current time:

Read More:   Baking transforms into SVG Path Element commands

var timeStamp = DateTime.Now.GetJavascriptTimeStamp();

Now in C# you can use built-in function:

new DateTimeOffset(Your_DateTime_Variable_Here).ToUnixTimeMilliseconds()

So the sample code would be:

var dateToUse = DateTime.Now;
var javaGetTimeValue = new DateTimeOffset(dateToUse).ToUnixTimeMilliseconds()


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts