Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert C# .NET DateTime.ticks to days/hours/mins in JavaScript

In my system, I am storing a duration in Ticks, which is being passed to my client mobile application, and from there I want to convert ticks into a human readable form. In my case, days, hours and minutes.

My client mobile application is coded using Javascript, and so this is what I'm using to convert the duration to days/hours/minutes.

like image 352
Ciaran Gallagher Avatar asked Mar 18 '13 20:03

Ciaran Gallagher


People also ask

How do you convert C to F fast?

If you find yourself needing to quickly convert Celsius to Fahrenheit, here is a simple trick you can use: multiply the temperature in degrees Celsius by 2, and then add 30 to get the (estimated) temperature in degrees Fahrenheit.

What does C equal in Fahrenheit?

C° to F°: Celsius to Fahrenheit Conversion Formula To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.

What does C mean in conversion?

Quick Celsius (°C) / Fahrenheit (°F) Conversion:°C.

What temperature in Fahrenheit is 50 C?

Answer: 50° Celsius is equal to 122° Fahrenheit.


3 Answers

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.

like image 115
Ciaran Gallagher Avatar answered Oct 06 '22 16:10

Ciaran Gallagher


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.

like image 45
pkmelee337 Avatar answered Oct 06 '22 15:10

pkmelee337


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.

like image 39
citykid Avatar answered Oct 06 '22 14:10

citykid