简体   繁体   中英

Dealing with time data, how do I convert this AS3 line to Javascript?

I have an old line of code for an AS3 application:

return Math.Round((DateTime.Now - new DateTime(1970, 1,1)).TotalMilliseconds);

However, I'm currently working in Javascript / Typescript. I tried to write what felt like a conversion, but it's just producing 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. To convert that to seconds, divide by 1,000. And, the Math.round() to get you seconds instead of milliseconds.

So, you were almost there, it's just that new Date().Now isn't a thing. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM