c# – How to convert JavaScript date object to ticks-ThrowExceptions

Exception or error:

How should I convert JavaScript date object to ticks? I want to use the ticks to get the exact date for my C# application after synchronization of data.

How to solve:

If you want to convert your DateTime object into universal ticks then use the following code snippet:

var ticks = ((yourDateObject.getTime() * 10000) + 621355968000000000);

There are 10000 ticks in a millisecond. And 621.355.968.000.000.000 ticks between 1st Jan 0001 and 1st Jan 1970.

###

The JavaScript Date type’s origin is the Unix epoch: midnight on 1 January 1970.

The .NET DateTime type’s origin is midnight on 1 January 0001.

You can translate a JavaScript Date object to .NET ticks as follows:

var yourDate = new Date();  // for example

// the number of .net ticks at the unix epoch
var epochTicks = 621355968000000000;

// there are 10000 .net ticks per millisecond
var ticksPerMillisecond = 10000;

// calculate the total number of .net ticks for your date
var yourTicks = epochTicks + (yourDate.getTime() * ticksPerMillisecond);

###

If by “ticks” you mean something like “milliseconds since the epoch”, you can call “.getTime()”.

var ticks = someDate.getTime();

From the MDN documentation, the returned value is an

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).

###

Expanding on the accepted answer as why 635646076777520000 is added.

Javascript new Date().getTime() or Date.now() will return number of milliseconds passed from midnight of January 1, 1970.

In .NET(source under Remarks sections)

The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar.

621355968000000000 is the value of ticks from midnight Jan 1 01 CE to midnight Jan 1 1970

So, in .NET

  Console.Write(new DateTime(621355968000000000))
  // output 1/1/1970 12:00:00 AM

Hence to convert javascript time to .Net ticks

 var currentTime = new Date().getTime();

 // 10,000 ticks in 1 millisecond
 // jsTicks is number of ticks from midnight Jan 1, 1970
 var jsTicks = currentTime * 10000;

 // add 621355968000000000 to jsTicks
 // netTicks is number of ticks from midnight Jan 1, 01 CE
 var netTicks = jsTicks + 621355968000000000;

Now, in .NET

 Console.Write(new DateTime(netTicks))
 // output current time

###

Date in JavaScript also contains offset. If you need to get rid of it use following:

return ((date.getTime() * 10000) + 621355968000000000) - (date.getTimezoneOffset() * 600000000);

I use Skeikh’s solution and subtract ticks for ‘offset’.

###

That should be date.GetTime(). Be aware that C# and Javascript using different initial dates so use something like this to convert to C# DateTime.

public static DateTime GetDateTime(long jsSeconds)
{
    DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return unixEpoch.AddSeconds(jsSeconds);
}

Leave a Reply

Your email address will not be published. Required fields are marked *