简体   繁体   English

处理时间数据时,如何将AS3行转换为Javascript?

[英]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.

相关问题 如何使用Jangaroo将单个As3函数转换为javascript? - How do I use Jangaroo to convert a single As3 function to javascript? Javascript中如何将一个字符串转换成可执行的代码行? - How do I convert a string into an executable line of code in Javascript? 如何将此JavaScript行转换为C#? - How do I convert this javascript line to c#? 如何将第6行和第7行从python转换为javascript? - How do I convert line 6 and 7 from python into javascript? 如何在JavaScript中将日期/时间转换为HST? - How do I convert a date/time to HST in JavaScript? 如何将天气 api 转换为当地时间(ReactJS/JavaScript) - How do I convert weather api to local time (ReactJS/JavaScript) 如何将 JavaScript 中的数据缓冲区转换为文件? - How do I convert a data buffer in JavaScript into a file? 如何将此功能从as3转换为javascript - How to convert this function from as3 to javascript 如何将用户的本地时间转换为多伦多时区,然后在 Javascript 中根据多伦多时间检查 dst? - How do I convert user's local time to Toronto timezone and then check dst based on Toronto time in Javascript? JavaScript:如何使用纯Javascript将UTC日期时间转换为EST小时? - JavaScript : How do i convert UTC Date Time to EST Hours using pure Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM