如何将时间戳转换为 javascript 日期对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5846625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:10:45  来源:igfitidea点击:

How do I turn a timestamp into a javascript date object?

javascriptjquery

提问by TIMEX

Possible Duplicate:
Convert a Unix timestamp to time in Javascript

可能的重复:
在 Javascript 中将 Unix 时间戳转换为时间

Turn a unix timestamp into 2008-07-17T09:24:17Z

将 Unix 时间戳转换为 2008-07-17T09:24:17Z

How to do that?

怎么做?

回答by Raze

Unix time stamp is in seconds since epoch right? You can convert it into milliseconds (by multiplying it by 1000) and passing it to the date constructor like this to convert it to a Date object.

Unix 时间戳是自纪元以来的秒数,对吗?您可以将其转换为毫秒(通过将其乘以 1000)并将其传递给这样的日期构造函数以将其转换为 Date 对象。

new Date(unixtimestamp*1000)

Then you can use the Date APIsto get parts of the date.

然后您可以使用日期 API来获取部分日期。

回答by otakustay

unix timestamp has an accuration of second, so convert to milisecondand pass to Date constructor:

unix 时间戳的精度为second,因此转换为毫秒并传递给 Date 构造函数:

var d = new Date(timestamp * 1000)

回答by Richard Neil Ilagan

As long as it's a valid date string, you should be able to get a Date object out of it using Date.parse().

只要它是一个有效的日期字符串,您就应该能够使用Date.parse().