Now.Ticks.ToString
这个问题没有确切的答案。你提出了两个问题:
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;
}
现在,getTicks(new Date())
将会得到与VB.Net中的Now.Ticks.ToString
相同的结果,误差为1毫秒。
var date = new Date();
var ticks = date.getTime();
getTime返回自1970年1月1日以来的毫秒数。
如果精度不是问题,尝试使用TimeSpan.TicksPerMillisecond
来表示您的计时器。
虽然不太好听,但这就是你需要的答案:http://codemonkey.joeuser.com/article/308527
DateTime.Ticks
表示自公元1年1月1日午夜12点以来已经过去的100纳秒间隔数。
JavaScript有Date.getTime()
函数,它测量自1970年1月1日以来的毫秒数。因此,如果您只需要一个唯一的值,那么可以使用该函数。显然,这与DateTime.Ticks
不能直接进行比较。
最快、最简单的版本是...
//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();