The example in this article describes the JavaScript method of calculating dates within two date time periods. Share it with everyone for your reference. The specific implementation method is as follows:
/***************************
* Calculate all dates within two date time periods
*
* @param value1
* Start date YYYY-MM-DD
* @param value2
* End date
* return date array
*/
function dataScope(value1, value2) {
var getDate = function(str) {
var tempDate = new Date();
var list = str.split("-");
tempDate.setFullYear(list[0]);
tempDate.setMonth(list[1] - 1);
tempDate.setDate(list[2]);
return tempDate;
}
var date1 = getDate(value1);
var date2 = getDate(value2);
If (date1 > date2) {
var tempDate = date1;
date1 = date2;
date2 = tempDate;
}
date1.setDate(date1.getDate() 1);
var dateArr = [];
var i = 0;
While (!(date1.getFullYear() == date2.getFullYear()
.getDate())) {
var dayStr =date1.getDate().toString();
If(dayStr.length ==1){
dayStr="0" dayStr;
dateArr[i] = date1.getFullYear() "-" (date1.getMonth() 1) "-"
dayStr;
i ;
/*
* document.write("
" date1.getFullYear()
* "-" (date1.getMonth() 1) "-" date1.getDate() "
");
*/
// document.write(dateArr[i] "
");
date1.setDate(date1.getDate() 1);
}
Return dateArr;
}
I hope this article will be helpful to everyone’s JavaScript programming design.