[英]substracting days to date loop
Here's the code : 这是代码:
var date = new Date(annee, mois, jour);
var i = 0;
while (i < 365) {
date.setTime(date.getTime()-(1000*60*60*24*i));
console.log(date.getFullYear()+'/'+parseInt(date.getMonth()+1)+'/'+date.getDate());
i++;
}
I'm trying to substract 1 day per loop, but i get this : 我正在尝试每个循环减去1天,但是我得到了:
2016/1/13
2016/1/11
2016/1/8
2016/1/4
2015/12/30
2015/12/24
2015/12/17
2015/12/9
2015/11/30
2015/11/20
[...]
1834/2/27
after a quick look on stack's solution, I found this : Finding date by subtracting X number of days from a particular date in Javascript But the result is always a bunch of false days ... I tried by getDate()-days
, setTime(getTime()-(different ms calculs))
- as in code -, trying with utc gmt and iso ... 快速浏览堆栈的解决方案后,我发现了这一点: 通过从Javascript中的特定日期中减去X天数来查找日期,但是结果始终是一堆错误的日子...我尝试了
getDate()-days
, setTime(getTime()-(different ms calculs))
- 如代码中所示 -尝试使用utc gmt和iso ...
well ... hope someone can help me :/ thanks ! 好吧...希望有人可以帮助我:/谢谢!
Remove multiplication by i. 用i删除乘法。 It subtracts i days.
它减去i天。
var date = new Date(annee, mois, jour);
var i = 0;
while (i < 365) {
date.setTime(date.getTime()-(1000*60*60*24)); //removed *i
console.log(date.getFullYear()+'/'+parseInt(date.getMonth()+1)+'/'+date.getDate());
i++;
}
Dates are really hard. 日期真的很难。 Even though it seems like an easy calculation, what happens if the date is the first of the month - would you expect the date to rollback to the last month?
即使看起来很简单,但是如果日期是该月的第一天该怎么办-您希望该日期回滚到最后一个月吗? What if it is the first day of the year?
如果是一年的第一天怎么办? Or the 1st March and it is a leap year?
还是3月1日,这是a年?
I'd recommend deferring tasks like this to a library such as http://momentjs.com/ or http://sugarjs.com/dates . 我建议将这样的任务推迟到http://momentjs.com/或http://sugarjs.com/dates之类的库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.