问题描述
我知道这个问题已经被问过很多次了,但是我无法将给定的毫秒数转换为日期和时间.
I know this question has been asked and answered many times, but I could not convert a given milliseconds to date and time.
我有以下代码:
var date = '1475235770601';
var d = new Date(date);
var ds = d.toLocaleString();
alert(ds);
console.log(ds);
运行此命令时,我会在控制台和警报中看到无效的日期.
When I run this I see Invalid Date in console as well as in alert.
但是当我在此处粘贴毫秒时,它会以本地日期和时间的格式很好地转换为Fri Sep 30 2016 17:12:50
.
But when I paste the milliseconds here, it converts fine in the format in local date and time as Fri Sep 30 2016 17:12:50
.
实际上我想以09/30/16 17:12:50
格式转换.
Actually I want to convert in in the format 09/30/16 17:12:50
.
推荐答案
Date
的构造方法使用数字,而不是字符串.要么直接输入:
Date
's constructor takes a number, not a string. Either feed it in directly:
date = 1475235770601; // Note the lack of quotes making it a number
或者,如果您已经有一个字符串,则将其显式转换:
Or, if you already have a string, convert it explicitly:
date = parseInt(date);
这篇关于如何将毫秒转换为日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!