Link to home
Start Free TrialLog in
Avatar of Abhilash Nagar
Abhilash Nagar

asked on

Ticks to DateTime conversion

Can anyone please suggest how to covert  ticks to DateTime in JavaScript or Jquery. For example below is the format
i am getting ticks

636891926215555850

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Abhilash Nagar
Abhilash Nagar

ASKER

Thanks ,that worked but the format is like below : Not sure if i can remove everything starting from GMT and till end.

Tue Mar 26 2019 10:11:33 GMT+1000 (Australian Eastern Standard Time)
Check this function. It returns an object with all you need so you can use each name-value pair.
function timeTicks(ticks){
   let ticksToMicrotime = ticks / 10000;
   let epochMicrotimeDiff = Math.abs(new Date(0, 0, 1).setFullYear(1));
   let tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);

  let date = tickDate.getDate();
  let month = tickDate.getMonth()+1; //Be careful! January is 0 not 1
  let year = tickDate.getFullYear();
  let hours=tickDate.getHours();
  let minutes=tickDate.getMinutes();
  let seconds=tickDate.getSeconds();
   return {
       date:date,
       month:month,
       year:year,
       hours:hours,
       minutes:minutes,
       seconds:seconds
   
   }
}
console.log(timeTicks(636891926215555850));

Open in new window

PS
You pass the ticks argument into function to get the object.
Thanks for the suggestion it is returning like below :
      [object Object]

Anything i need to do here further please thanks.
Thanks everyone , this helped me to fix. I am able to remove GMT from the first suggestion but both solutions are good to use.Thanks again.
Date and time is not correct here it seem.I am getting :

Tue, 23 Apr 2019 23:38:42

correct value should be:

Tue, 24 Apr 2019 09:50:00

Still debugging to find out more, any ideas on this? Thanks.
Try this:

var ticks =636891926215555850;

//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(0));

//new date is ticks, converted to microtime, minus difference from epoch microtime
var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);
Thanks for the response, Just tried this but it is giving now same date for all records which is :

      Sun, 25 Mar 2018 00:11:33
Please, try with UTC

  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(Date.UTC(0, 0, 1)).setFullYear(1));
  //new date is ticks, converted to microtime, minus difference from epoch microtime
  var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);

And, are you getting datetime value
Tue, 23 Apr 2019 23:38:42
for ticks value
636891926215555850
?
Yes thats correct. I am getting this value but if you will check here :

https://tickstodatetime.azurewebsites.net/

the correct value should be :
26-03-2019 10:23:41
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the response. I will retry again as per the link you provided, will update you soon.

https://jsfiddle.net/pkmelee337/gwL5up4j/3/


Thanks.
Yes you are right now date is coming correct. I modified my script to match with link you provided but the format is coming like below :

Tue Mar 26 2019 20:11:33 GMT+1000 (Australian Eastern Standard Time)

Can we format this like below please :

Tue, 26 Mar 2019 20:11:33
Thanks now it is coming correct. I formatted the string like below
"Tue Mar 26 2019 20:11:33 GMT+1000 (Australian Eastern Standard Time)"

currDateString.toString().substring(0,currDateString.toString().indexOf('GMT')), which gives me:

Tue, 26 Mar 2019 20:11:33

All looks fine now thanks again.
Thanks for the solution.
Hello again,
 Thanks for your time ,i just tried this one again :

https://jsfiddle.net/pkmelee337/gwL5up4j/3/

with .NET  ticks getting from service : 636930037440175900

I get below result -

Thu May 09 2019 22:50:16

But when i try same in the below link :

https://tickstodatetime.azurewebsites.net/

I get this -
2019-05-09​ 13:02:24

which is
Thu May 09 2019 13:02:24  which is correct as it matched the .NET one. It seems there is some difference between .NET ticks and Javascript ones, trying to figure out again.

Any suggestions will help me further thanks.
That is fine i will look into it please thanks, closing this as it is.Thanks.