Why we multiply seconds with 1000 in Javascript to add time

依然范特西╮ 提交于 2021-02-05 08:00:06

问题


I am working on cookies and I found some questions on Stack Overflow which have been answered. I see in the questions that the answers multiplied seconds by 1000. I want know what format getTime() returns, that would require multiplying by 1000.

Make a cookie expire in 30 seconds

How to set a cookie to expire in 1 hour in Javascript?


回答1:


JavaScript uses milliseconds to represent epoch time.

Epoch time is the number of seconds since 01/01/1970 and there are 1000 milliseconds in one second. So to get the number of milliseconds since 01/01/1970 you just multiply the value by 1000.

Reference -

  • MDN - Date



回答2:


Because it's given to you in milliseconds.

Seconds * 1000 = Milliseconds



回答3:


getTime

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC




回答4:


It's using milliseconds. That's why we need to multiply the seconds with 1000 to get an equivalent value.



来源:https://stackoverflow.com/questions/19363506/why-we-multiply-seconds-with-1000-in-javascript-to-add-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!