Convert C# .NET DateTime.ticks to days/hours/mins in JavaScript
In C# .NET, a single tick represents one hundred nanoseconds, or one ten-millionth of a second. [Source].
Therefore, in order to calculate the number of days from the number of ticks (rounded to nearest whole numbers), I first calculate the number of seconds by multiplying by ten million, and then multiplying that by the number of seconds in a day (60 seconds in minute, 60 minutes in hour, 24 hours in day). I use the modulus operator (%) to get the remainder values that make up the duration of hours and minutes.
var time = 3669905128; // Time value in ticks
var days = Math.floor(time/(24*60*60*10000000)); // Math.floor() rounds a number downwards to the nearest whole integer, which in this case is the value representing the day
var hours = Math.round((time/(60*60*10000000)) % 24); // Math.round() rounds the number up or down
var mins = Math.round((time/(60*10000000)) % 60);
console.log('days: ' + days);
console.log('hours: ' + hours);
console.log('mins: ' + mins);
So, in the above example, the amount of ticks is equivalent to 6 minutes (rounded up).
And to take another example, with 2,193,385,800,000,000 ticks, we get 2538 days, 15 hours and 23 minutes.
var ticks = 635556672000000000;
//ticks are in nanotime; convert to microtime
var ticksToMicrotime = ticks / 10000;
//ticks are recorded from 1/1/1; get microtime difference from 1/1/1/ to 1/1/1970
var epochMicrotimeDiff = Math.abs(new Date(0, 0, 1).setFullYear(1));
//new date is ticks, converted to microtime, minus difference from epoch microtime
var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);
According to this page the setFullYear method returns "A Number, representing the number of milliseconds between the date object and midnight January 1 1970".
Check out this page for all the methods from the javascript Date object.
You need to consider 2 things:
Resolution
Ticks in .Net's DateTime are 0.1 Microsecond, while Javascript counts Milliseconds.
Offset
In addition, .Net counts from 1.1.0000 while Javascript counts from 1.1.1970.
TeaFiles.Net has a Time class that uses Java = Javascript ticks. It has a scale property and a predefined Timescale.Java scale, that converts from .Net to Javascript.