Reputation: 611
How can I replicate this in javascript?
Now.Ticks.ToString
Upvotes: 4
Views: 3121
Reputation: 63676
The quickest, simplest version would be...
//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();
Upvotes: 0
Reputation: 583
Try to use TimeSpan.TicksPerMillisecond
for your ticks if accuracy is not a problem.
Upvotes: 1
Reputation: 19057
There is no real equivalent. You ask two questions:
How do I get the current date & time in javascript? This is easy, just write
var now = new Date();
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.
Upvotes: 8
Reputation: 14906
It's not pleasant, but here's your answer: http://codemonkey.joeuser.com/article/308527
DateTime.Ticks
represent the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1st, 0001.
JavaScript has Date.getTime()
which measures the number of milliseconds since January 1st, 1970, so if you're just after something that's unique then you could use that. Obviously that's not directly comparable to DateTime.Ticks
though.
Upvotes: 0
Reputation: 60095
var date = new Date();
var ticks = date.getTime();
getTime returns the number of milliseconds since January 1, 1970
Upvotes: 4