我同意 Thilo(本地化到时区),但我可能会这样处理:
// Original: Thu Jun 21 2012 19:58:20 GMT-0400 (Eastern Daylight Time)
var ms = 1340323100024;
var msPerDay = 86400 * 1000;
var beginning = ms - (ms % msPerDay);
// Result: Wed Jun 20 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
或者,如果您愿意:
Number.prototype.StartOfDayMilliseconds = function(){
return this - (this % (86400 * 1000));
}
var ms = 1340323100024;
alert(ms.StartOfDayMilliseconds());
编辑
如果您特别关注时区,可以使用:
// Original: Thu Jun 21 2012 19:58:20 GMT-0400 (Eastern Daylight Time)
var ms = 1340323100024;
var msPerDay = 86400 * 1000;
var beginning = ms - (ms % msPerDay);
beginning += ((new Date).getTimezoneOffset() * 60 * 1000);
// Result: Thu Jun 21 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
请注意,现在删除了偏移量,因此前一天晚上 8 点变成时间戳上实际日期的午夜。您也可以(取决于实现)在您对一天开始的取模之前或之后进行加法 - 您的偏好。