How to calculate a date in two date periods in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
Tags date1
This article describes how to use JavaScript to calculate the date in two date periods, involving the time of javascript operations, for more information about how to use JavaScript to calculate dates in two date periods, see the following example. Share it with you for your reference. The specific implementation method is as follows:

The Code is as follows:

/*************************
* Calculate all dates in two date 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 ()
& Amp; date1.getMonth () = date2.getMonth () & amp; date1.getDate () = date2
. 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 help you design javascript programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.