[英]Dealing with time data, how do I convert this AS3 line to Javascript?
I have an old line of code for an AS3 application: 我为AS3应用程序编写了一行旧代码:
return Math.Round((DateTime.Now - new DateTime(1970, 1,1)).TotalMilliseconds);
However, I'm currently working in Javascript / Typescript. 但是,我目前正在使用Javascript / Typescript。 I tried to write what felt like a conversion, but it's just producing NaN. 我试图写那种感觉像是一次转换,但它只是在产生NaN。
getTime() {
return Math.round((new Date().Now - new Date().getTime()) / 1000);
}
How can I fix this small snippet so that it produces the same results? 如何修复此小片段,以便产生相同的结果?
If I'm understanding what you want to do, it's as simple as this: 如果我了解您想要做什么,就这么简单:
new Date().getTime() // For Milliseconds
Math.round(new Date().getTime() / 1000) // For Seconds
.getTime()
returns the milliseconds since the epoch of January 1, 1970 UTC. .getTime()
返回自1970年1月1日UTC以来的毫秒数。 To convert that to seconds, divide by 1,000. 要将其转换为秒,请除以1,000。 And, the Math.round()
to get you seconds instead of milliseconds. 而且, Math.round()
可以让您获得秒数而不是毫秒数。
So, you were almost there, it's just that new Date().Now
isn't a thing. 因此,您几乎就在那儿,只是new Date().Now
而已。 :-) :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.