How to Convert JavaScript Date Object to Ticks

How to convert JavaScript date object to ticks

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.

How to get date ticks in a javaScript?

  • JavaScript: Date.getTime(): a number, representing the number of milliseconds since midnight January 1, 1970.

  • C#: A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.

So, JavaScript ticks = (C# ticks / 10000) and your code looks OK. You just need to account for the difference in whichever code (C# or JavaScript) you choose.

If you take (an unambiguous day-month date) in JavaScript (e.g. 23 December 2018):

var $input = "23.12.2018";
var from = $input.split(".");

var dNew = new Date(from[2], from[1]-1, from[0]);
//Note dStart has 0 as month because JavaScript dates start at 0 and end with 11
var dStart = new Date(1970, 0, 1);
var seconds = dNew.getTime();
var secondsStart = dStart.getTime();
var dateDifference = seconds - secondsStart;
// multiply by 10000 to reconcile to c#
console.log("===> " + dateDifference * 10000);

Both answers come to 15455232000000000 if you use StringDateToDecimal("23 december 2018"); in your C# code.

I hope I'm getting the jist of what you are asking.

Date format \/Date(1394841600000)\/?(In ticks)

It is date in ticks since midnight Jan 1, 1970 to the date of object.

To convert use:

var myDate = new Date("/Date(1394841600000)/".match(/\d+/)[0] * 1);

Here is Demo

.Net Ticks to ISO 8601 Date format using JavaScript

The epoch for the DateTimeOffset type is 0000-01-01, while the epoch for Javascript dates is 1970-01-01.

The DateTimeOffset ticks value for the date 1970-01-01 is 621355968000000000, so you can just subtract that from the value to convert it to the Javascript epoch.

Ticks in DateTimeOffset is 1/10000000 second, while ticks in Javascript dates is 1/1000 second, so divide the value by 10000 to convert it to Javascript date ticks.

So, to get the Javascript date from the DateTimeOffset ticks:

new Date((date[0] - 621355968000000000) / 10000)

Then you can use the toISOString method to convert the date to an ISO8601 string.

Conversion of date into long format, How it works?

The long format displays the number of ticks after 01.01.1970, so for now its about 43 years.

* operator forces argument to be cast to number, I suppose, Date object has such casting probably with getTime().

You double the number of milliseconds - you get 43 more years, hence the 2057 (or so) year.

How to convert Javascript datetime to C# datetime?

First create a string in your required format using the following functions in JavaScript

var date = new Date();
var day = date.getDate(); // yields date
var month = date.getMonth() + 1; // yields month (add one as '.getMonth()' is zero indexed)
var year = date.getFullYear(); // yields year
var hour = date.getHours(); // yields hours
var minute = date.getMinutes(); // yields minutes
var second = date.getSeconds(); // yields seconds

// After this construct a string with the above results as below
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;

Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact() in codebehind to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Hope this helps...



Related Topics



Leave a reply



Submit