Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB Now.Ticks equiv in javascript

How can I replicate this in javascript?

Now.Ticks.ToString
like image 325
Oppdal Avatar asked Dec 13 '22 15:12

Oppdal


1 Answers

There is no real equivalent. You ask two questions:

  1. How do I get the current date & time in javascript? This is easy, just write

    var now = new Date();
    
  2. How do I get the number of ticks since Januari 1, 0001? This one is harder, because javascript doesn't work with ticks, but with milliseconds, and the offset is Januari 1, 1970 instead.

    You can start with now.getTime() to get the milliseconds since Jan 1, 1970, and then multiply this with 10000. I just calculated the number of ticks between 0001-01-01 and 1970-01-01, and this is 621355968000000000. If you also take into account the timezone, the resulting code looks like this:

    function getTicks(date)
    {
        return ((date.getTime() - date.getTimezoneOffset() * 60000) * 10000) + 621355968000000000;
    }
    

Now getTicks(new Date()) will get the same result as Now.Ticks.ToString in VB.Net with an error margin of 1 millisecond.

like image 76
Elian Ebbing Avatar answered Dec 31 '22 01:12

Elian Ebbing