admin管理员组文章数量:1431426
I am having an issue and was wondering if someone could give me a little insight as to why. The following code is working to add one week to a date:
while (c.getStamp() < b.getStamp()) {
var f = this.getWeek(c);
e.push(f);
c = (c.getStamp() + 604800).toDate();
}
...
Date.prototype.getStamp = function() {
return Math.round(this.getTime() / 1e3);
};
Number.prototype.toDate = function() {
return new Date(this * 1e3);
};
I'm trying to get the following to work but it creates a continuous loop:
while (c.getStamp() < b.getStamp()) {
var f = this.getWeek(c);
e.push(f);
c = new Date(c.getFullYear(), c.getMonth(), c.getDate + 7, 0, 0, 0);
}
Where c = JS Date, ie 05/01/12
and b = JS Date, ie 05/31/12
I am having an issue and was wondering if someone could give me a little insight as to why. The following code is working to add one week to a date:
while (c.getStamp() < b.getStamp()) {
var f = this.getWeek(c);
e.push(f);
c = (c.getStamp() + 604800).toDate();
}
...
Date.prototype.getStamp = function() {
return Math.round(this.getTime() / 1e3);
};
Number.prototype.toDate = function() {
return new Date(this * 1e3);
};
I'm trying to get the following to work but it creates a continuous loop:
while (c.getStamp() < b.getStamp()) {
var f = this.getWeek(c);
e.push(f);
c = new Date(c.getFullYear(), c.getMonth(), c.getDate + 7, 0, 0, 0);
}
Where c = JS Date, ie 05/01/12
and b = JS Date, ie 05/31/12
-
Wow, I've never actually seen someone use
1e3
when not playing code golf. – Ry- ♦ Commented May 31, 2012 at 2:17
3 Answers
Reset to default 5You missed the ()
after c.getDate
.
Aside from that, you can just do this:
c.setTime(c.getTime()+7*24*60*60*1000); // adds 1 week to the date
To add a week to a date, the simplest method is to add 7 days:
var now = new Date();
// add one week exactly
now.setDate(now.getDate() + 7);
If you add the equivalent of 7*24hrs in milliseconds, you will be incorrect if the week crosses a daylight saving boundary. And the above is a bit clearer in the code.
In your code:
> while (c.getStamp() < b.getStamp()) {
if c
and d
are both date objects, then:
while (c < b) {
is more efficient, less error prone and less to write.
> var f = this.getWeek(c);
What is f
? What does f.getWeek
return?
> e.push(f);
> c = (c.getStamp() + 604800).toDate();
Presumably you want to add one week to c, so:
c.setDate(c.getDate() + 7);
Later...
> c = new Date(c.getFullYear(), c.getMonth(), c.getDate + 7, 0, 0, 0);
> --------------------------------------------------------^
You have a syntax error and have not zeroed the milliseconds. This seems like an even longer-winded way to a a week to c
, see above.
Where c = JS Date, ie 05/01/12 and b = JS Date, ie 05/31/12
Please note that this in an international forum, regional specific expressions should be avoided or explained if used. Are the above in the US–specific mm/dd/yyyy format? A more widely recognised format is dd/mm/yyyy or better to use an ISO8601 format: yyy-mm-dd (which should be supported natively by all browsers pliant with ES5, but they all aren't yet).
Oh, and the getStamp
and toDate
methods seem like attempts at rounding to the nearest second. You might try a function that does just that in one go:
Date.prototype.roundToSecond = function() {
this.setMilliseconds(this.getMilliseconds() > 499? 1000 : 0);
}
Kolink has the right answer, but you might also want to take a look at the Moment.js date library if your doing alot of work with manipulating dates and formatting them.
本文标签: javascriptJS Date issues with adding 1 weekStack Overflow
版权声明:本文标题:javascript - JS Date issues with adding 1 week - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745558458a2663327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论