本文介绍了Javascript一周一周的计算日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy guys guys,$ $ $。。。。。。。。。。。。。。。。。。。。。。。。。
但是当我的代码必须计算月末时,我出现错误:示例在31/07 / 2'15之后代码生成32/07/2015而不是01/08/2015。
这是我的代码:

Hy guys,i have write this javascript code for calculate date for any day of week progressively.But i ahve error when the code must be calculate the end of month: example after 31/07/2'15 the code generate 32/07/2015 instead 01/08/2015.This is my code:

var application = this;
        var currentDate = new Date();
        var stringDate1 = currentDate.getUTCDate() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate2 = (currentDate.getUTCDate()+1) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate3 = (currentDate.getUTCDate() + 2) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate4 = (currentDate.getUTCDate() + 3) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate5 = (currentDate.getUTCDate() + 4) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate6 = (currentDate.getUTCDate() + 5) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
        var stringDate7 = (currentDate.getUTCDate() + 6) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();

我的代码错了?
有什么帮忙吗?
BR

where my code is wrong?Any help please?BR

推荐答案

要计算前一天的日期,您可以使用

To calculate the date for one day ahead you can use Date.prototype.setDate()

所以要计算从今天到前一周的所有日期,您可以这样做:

So to calculate all dates from today and week ahead, you can do something like this:

var currentDate = new Date();
var stringDate = [];

for (var i = 0; i < 7; ++i) {
    currentDate.setDate(currentDate.getDate() + 1);
    stringDate[i] = currentDate.getUTCDate() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
}

console.log(stringDate);

// 0: "28/7/2015"
// 1: "29/7/2015"
// 2: "30/7/2015"
// 3: "31/7/2015"
// 4: "1/8/2015"
// 5: "2/8/2015"
// 6: "3/8/2015"

这篇关于Javascript一周一周的计算日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 12:09