88

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.

Dan Homola
  • 3,819
  • 1
  • 28
  • 43
Talha
  • 18,898
  • 8
  • 49
  • 66
  • 1
    Bear in mind that the date/time of the browser may be set to any time-zone so your data will not be relative to UTC on the server. I normally compensate by injecting the server's `utcnow` in ticks (relative to 1970) into the page and storing the difference between that value and `+new Date()` at page start. Then all date/time can be offset by that amount. Any error is then just down to the page latency. – iCollect.it Ltd Jan 21 '15 at 11:13

6 Answers6

134

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.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Sheikh Ali
  • 1,514
  • 1
  • 10
  • 8
  • 5
    This gets the ticks when you are in C#. See pointy's answer below for a javascript way. – mark Sep 26 '13 at 19:33
  • 2
    the added number doesn't work with me it always give me time:21:00 in previous day , but this one (621356076000000000) is worked fine, any Idea?? – MoniR Sep 03 '16 at 05:34
  • in Google Chrome 56.0.2924.87 (64-bit) the correct value is (621356076000000000), so what the difference between these two values? – okarpov Feb 16 '17 at 12:17
  • What programming language is your answer for - c# or JavaScript? I.e., where is this implemented? – barrypicker Feb 19 '18 at 20:27
  • @muniR I think it's because you mixed UTC and local time. 621355968000000000 is UTC. Your number 621356076000000000 would be so its works with your local time (you are 3 hours behind UTC time in your timezone) – sboisse Apr 20 '18 at 03:58
63

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);
LukeH
  • 263,068
  • 57
  • 365
  • 409
39

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

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 13
    ticks != milliseconds , so in C# A single tick represents one hundred nanoseconds – amd Feb 10 '13 at 19:26
14

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
Sami
  • 3,926
  • 1
  • 29
  • 42
10

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'.

michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
2

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);
}
Pieter
  • 3,339
  • 5
  • 30
  • 63