简单的javascript日期数学...不是真的 [英] Simple javascript date math... not really
问题描述
我正在尝试创建一个简单的脚本,从2011年7月6日(星期三)开始,每两周一次的计划就给我下一个回收日期。所以我创建了这个简单的函数...
function getNextDate(startDate){
if(today< = startDate){
return startDate;
}
//计算自开始日期起的日期。
var totalDays = Math.ceil((today.getTime() - startDate.getTime())/(one_day));
//检查这天是否在回收日
var bumpDays = totalDays%14; // mod 14 - 每14天起...
// pick is today
if(bumpDays == 0){
return today;
}
//返回14天内最接近的日期,减去最后一个
//接收后的天数。
var ms = today.getTime( )+((14-bumpDays)* one_day);
返回新日期(ms);
}
,可以像...一样调用...
var today = new Date();
var one_day = 1000 * 60 * 60 * 24; //以毫秒计的一天
var nextDate = getNextDate(new Date(2011,06,06));
到目前为止这么好...但是当我将今天项目推广到10/27/2011时,我将于2011年8月11日(星期二)作为下一个日期,而不是2011年11月9日星期三...事实上,从现在开始至2011年10月26日的每一天都会提供正确的接送服务,每个日期从10 / 27/2011至2/28/2012项目周二而不是星期三。然后每个日期从2012年2月29日(闰年)到2010年12月24日(嗯10月)再次对周三进行投射。我失踪了什么任何帮助将不胜感激。
V
最简单的方法是使用 setDate
来更新 Date
对象。由于此回答的意见表明这不是规范的正式部分,而是所有主流浏览器都支持。
您应该永远不会 getDate 通话的对象不同的 Date
对象。
示例实现
var incrementDate = function(date,amount ){
var tmpDate = new Date(date);
tmpDate.setDate(tmpDate.getDate()+ amount)
return tmpDate;
};
如果您尝试增加日期,请使用此功能。它将接受正值和负值。它还保证使用的日期对象不被更改。如果您不希望更新更改对象的值,则可以防止发生任何错误。
使用不正确
var startDate = new Date('2013-11-01T11:00:00 ');
var a = new Date();
a.setDate(startDate.getDate()+ 14)
这将更新根据 a
的值,以14天计算 startDate
的日期值。因为 a
的值与先前定义的 startDate
不一样,所以可能会收到错误的值。 p>
I am trying to create a simple script that gives me the next recycling date based on a biweekly schedule starting on Wed Jul 6, 2011. So I've created this simple function...
function getNextDate(startDate) {
if (today <= startDate) {
return startDate;
}
// calculate the day since the start date.
var totalDays = Math.ceil((today.getTime()-startDate.getTime())/(one_day));
// check to see if this day falls on a recycle day
var bumpDays = totalDays%14; // mod 14 -- pickup up every 14 days...
// pickup is today
if (bumpDays == 0) {
return today;
}
// return the closest day which is in 14 days, less the # of days since the last
// pick up..
var ms = today.getTime() + ((14- bumpDays) * one_day);
return new Date(ms);
}
and can call it like...
var today=new Date();
var one_day=1000*60*60*24; // one day in milliseconds
var nextDate = getNextDate(new Date(2011,06,06));
so far so good... but when I project "today" to 10/27/2011, I get Tuesday 11/8/2011 as the next date instead of Wednesday 11/9/2011... In fact every day from now thru 10/26/2011 projects the correct pick-up... and every date from 10/27/2011 thru 2/28/2012 projects the Tuesday and not the Wednesday. And then every date from 2/29/2012 (leap year) thru 10/24/2012 (hmmm October again) projects the Wednesday correctly. What am I missing? Any help would be greatly appreciated..
V
The easiest way to do this is update the Date
object using setDate
. As the comments for this answer indicate this isn't officially part of the spec, but it is supported on all major browsers.
You should NEVER update a different Date
object than the one you did the original getDate
call on.
Sample implementation:
var incrementDate = function (date, amount) {
var tmpDate = new Date(date);
tmpDate.setDate(tmpDate.getDate() + amount)
return tmpDate;
};
If you're trying to increment a date, please use this function. It will accept both positive and negative values. It also guarantees that the used date objects isn't changed. This should prevent any error which can occur if you don't expect the update to change the value of the object.
Incorrect usage:
var startDate = new Date('2013-11-01T11:00:00');
var a = new Date();
a.setDate(startDate.getDate() + 14)
This will update the "date" value for startDate
with 14 days based on the value of a
. Because the value of a
is not the same is the previously defined startDate
it's possible to get a wrong value.
这篇关于简单的javascript日期数学...不是真的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!