Javascript每周计算日期
嗨,伙计们, 我已经为计算一周中任何一天的日期编写了这个javascript代码。 但当代码必须在月末计算时,我有一个错误:例如,2015年7月31日之后,代码生成的是2015年7月32日,而不是2015年8月1日。 这是我的代码:Javascript每周计算日期,javascript,date,Javascript,Date,嗨,伙计们, 我已经为计算一周中任何一天的日期编写了这个javascript代码。 但当代码必须在月末计算时,我有一个错误:例如,2015年7月31日之后,代码生成的是2015年7月32日,而不是2015年8月1日。 这是我的代码: var application = this; var currentDate = new Date(); var stringDate1 = currentDate.getUTCDate() + "/" + (currentDate
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只需将
小时*分钟*秒*毫秒添加到今天的日期,就可以得到明天的日期,依此类推
var tomorrowsDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
要计算提前一天的日期,可以使用
因此,要计算今天和下周的所有日期,您可以执行以下操作:
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"
var currentDate=新日期();
var stringDate=[];
对于(变量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"
您是否正在尝试获取今天和未来6天的日期?无论哪种方式,您试图实现这一点的方式都太复杂了。是的,我的代码计算今天和未来6天的日期。好的,谢谢,但我每天都需要一个变量字符串,因此我必须使用数组创建6个变量。示例:stringDate1=stringDate(0),stringDate2=stringDate(1)等etc@dev_只要像今天的stringDate[0]和明天的stringDate[1]这样访问它,我已经将数组与singles变量相关联。谢谢你的帮助