Home > Web Front-end > JS Tutorial > body text

JavaScript method to calculate dates within two date time periods_javascript skills

WBOY
Release: 2016-05-16 16:09:10
Original
1476 people have browsed it

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:

Copy code The code 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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!