[英]How to convert a millisecond in a readable date format in javascript
I am trying to convert milliseconds (which i am getting from an api which returns JSN data) to a readable date format. 我正在尝试将毫秒(我从返回JSN数据的api中获取)转换为可读的日期格式。 The sample code is below - 示例代码如下-
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var d = new Date(Number("1429894800"));
document.getElementById("demo").innerHTML = d.toString();
</script>
</body>
</html>
It returns - Sat Jan 17 1970 07:11:34 GMT-0600 (CST). 它将返回-1970年1月17日星期六格林尼治标准时间0600(CST)。 But it should be the current date time. 但这应该是当前日期时间。 How to do the conversion? 怎么做转换?
If you want, approximately, the time "now" you would need to use: 如果需要大约“现在”的时间,则需要使用:
var d = new Date(1429894800000);
(Notice I removed the Number("...")
because you don't need to convert a string to a number when you can just use a number.) (请注意,我删除了Number("...")
因为只要使用数字就不需要将字符串转换为数字。)
which outputs, for me: 对我来说,输出:
Date {Fri Apr 24 2015 13:00:00 GMT-0400 (EDT)}
But a better way to determine "now" is using: 但是确定“现在”的更好方法是使用:
Date.now();
For lots more information, go to a handy dandy page on the Date
class here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 有关更多信息,请转到Date
类上方便的丹迪页面: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.