Javascript:如何用毫秒计算一天的开始? [英] Javascript: how to calculate the beginning of a day with milliseconds?

查看:1628
本文介绍了Javascript:如何用毫秒计算一天的开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚从一天的开始,给出了一个毫秒的时间。



所以说我给了这个:1340323100024这就像中午6/21/2012。现在我想要从一天开始的毫秒,这将是1340262000000(至少我认为这是应该是这样)。



我如何获得1340262000000从1340323100024?



我尝试过

  Math.floor(1340323100024 / 86400000)* 86400000 

但这给了我1340236800000,如果我创建一个日期对象,说它的第20个。



我知道我可以从1340323100024创建一个日期对象,然后获取月,年和日期,以创建一个新的对象,这将给我1340262000000,但是我觉得这很可笑,我无法弄清楚如此简单的事情。



任何帮助将不胜感激。


解决方案

我同意Thilo(本地化到时区),但我可能会这样解决:

  //原文:2012年4月21日19:58:20 GMT-0400(东部夏令时间)
var ms = 1340323100024;
var msPerDay = 86400 * 1000;
var starts = ms - (ms%msPerDay);
//结果:Wed Jun 20 2012 20:00:00 GMT-0400(Eastern Daylight Time)

或者,如果你喜欢:

  Number.prototype.StartOfDayMilliseconds = function(){
return this - (本%(86400 * 1000));
}

var ms = 1340323100024;
alert(ms.StartOfDayMilliseconds());

编辑



如果你特别关于时区,你可以使用:

  //原文:2012年4月21日19:58 :20 GMT-0400(Eastern Daylight Time)
var ms = 1340323100024;
var msPerDay = 86400 * 1000;
var starts = ms - (ms%msPerDay);
开始+ =((新日期).getTimezoneOffset()* 60 * 1000);
//结果:2012年3月21日00:00:00 GMT-0400(东部夏令时)

请注意,偏移量现在已被删除,因此前一天晚上8点将转入实际日期的午夜。您也可以(取决于实现)在一天开始之前或之后添加您的偏好。


i want to figure out the time from the beginning of the day given a days milliseconds.

so say i'm given this: 1340323100024 which is like mid day of 6/21/2012. now i want the milliseconds from the beginning of the day, which would be 1340262000000 (at least i think that's what it's supposed to be.)

how do i get 1340262000000 from 1340323100024?

i tried doing

Math.floor(1340323100024/86400000) * 86400000 

but that gives me 1340236800000, which if i create a date object out of it, says its the 20th.

i know i can create a date object from 1340323100024, then get the month, year, and date, to create a new object which would give me 1340262000000, but i find it ridiculous i can't figure out something so simple.

any help would be appreciated.

btw, i'm doing this in javascript if it makes any difference.

解决方案

I agree with Thilo (localized to time zone), but I'd probably tackle it like this:

// 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)

Or, if you prefer:

Number.prototype.StartOfDayMilliseconds = function(){
  return this - (this % (86400 * 1000));
}

var ms = 1340323100024;
alert(ms.StartOfDayMilliseconds());

EDIT

If you're particular about the timezone, you can use:

// 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)

Notice that the offset is now removed so the 8pm the previous day turns in to midnight of the actual day on the timestamp. You can also probably (depending on implementation) do the addition before or after you modulo for the beginning of the day--your preference.

这篇关于Javascript:如何用毫秒计算一天的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆