VB Now.Ticks equiv in javascript

How can I reproduce this in javascript?

Now.Ticks.ToString 
+4
source share
5 answers

This is not nice, but here is your answer: http://codemonkey.joeuser.com/article/308527

DateTime.Ticks represents the number of 100-nanosecond intervals from 12:00 to midnight, January 1, 0001.

JavaScript has Date.getTime() , which measures the number of milliseconds since January 1, 1970, so if you are only after something unique, then you can use this. Obviously, this is not directly comparable to DateTime.Ticks .

0
source

There is no real equivalent. You ask two questions:

  • How to get current date and time in javascript? It's easy, just write

     var now = new Date(); 
  • How to get the number of ticks with Januari 1, 0001? This is more complicated because javascript does not work with ticks, but with milliseconds, and the offset is Januari 1, 1970.

    You can start with now.getTime() to get the milliseconds from January 1, 1970, and then multiply this by 10000. I just counted the number of ticks between 0001-01-01 and 1970-01-01, and that is 621355968000000000. If you also take into account the time zone, the resulting code is as follows:

     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 limit of 1 millisecond.

+8
source
 var date = new Date(); var ticks = date.getTime(); 

getTime returns the number of milliseconds since January 1, 1970.

+4
source

Try using TimeSpan.TicksPerMillisecond for your ticks if accuracy is not a problem.

+1
source

The fastest, easiest version is ...

 //get string version of time to the nearest millisecond var now = "" + new Date().getTime(); //Though in most cases its easier to keep it as a number, and just concatinate in your output somewhere document.title = "Now it is: " + new Date().getTime(); 
0
source

Source: https://habr.com/ru/post/1343945/


All Articles