Js gets all the days from the start and end time, and js End Time

Source: Internet
Author: User

Js gets all the days from the start and end time, and js End Time


Today, I saw a post in the forum. We calculated the date of each day at the interval given the input and end of the signed date. Just because the project is not busy, you can write it and record it.


Original problem

Var start_time = "2015-2-1"
Var end_time = "2015-3-1"

Return all days between the request start time and end time ['2017-2-1 ', "2015-2-2"... "2-2-28", "2015-3-1"]


Method 1


<Script> // obtain the number of days interval function getDays (day1, day2) {// obtain the Date type Date of the input parameter string form var d1 = day1.getDate (); var d2 = day2.getDate (); // defines the number of milliseconds in a day. var dayMilliSeconds = 1000*60*60*24; // obtains the number of milliseconds of the input date. var d1Ms = d1.getTime (); var d2Ms = d2.getTime (); // defines the return value var ret; // compare the number of milliseconds in the date until the d1Ms is greater than or equal to d2Ms, and exit the loop // The end of each loop, add a day for (d1Ms; d1Ms <= d2Ms; d1Ms + = dayMilliSeconds) to d1Ms {// if ret is empty, you do not need to add "," as the separator if (! Ret) {// convert the number of milliseconds to Date var day = new Date (d1Ms); // obtain the string ret = day in the form of year, month, and day. getYMD ();} else {// otherwise, add "," as the separator var day = new Date (d1Ms); ret = ret + 'for each ret character day ', '+ day. getYMD () ;}} alert (ret); // or return ret;} // Add the getYMD Method to the Date object to obtain the Date in the string format. prototype. getYMD = function () {var retDate = this. getFullYear () + "-"; // obtain the year. RetDate + = this. getMonth () + 1 + "-"; // obtain the month. RetDate + = this. getDate (); // get the day. Return retDate; // return date .} // Add the getDate Method to the String object so that the Date in String form is returned as a Date String. prototype. getDate = function () {var strArr = this. split ('-'); var date = new Date (strArr [0], strArr [1]-1, strArr [2]); return date ;} getDays ('2017-2-1 ', '2017-3-1'); </script>


In the preceding method, the start date is auto-incrementing by the number of milliseconds to avoid calculating the number of days each month.

GetYMD and getDate are the methods added on the Date and String prototypes respectively for our convenience.



Method 2


Open the chrome console


We can see that the getTime values for the second and second days of the second day are equal.

<Script> // obtain the number of days interval function getDays (day1, day2) {// obtain the Date-type Date var st = day1.getDate () of the input parameter string format (); var et = day2.getDate (); // defines the returned array var retArr = []; // loop. When the start date is not equal to the end date, the loop while (st. getTime ()! = Et. getTime () {// store the date in the string format of the start date in the array retArr. push (st. getYMD (); // get the day var tempDate = st. getDate (); // point the start Date st to the new Date constructed. // Add one day to the new Date (st. getFullYear (), st. getMonth (), st. getDate () + 1);} // Add the day of the end date to the array retArr. push (et. getYMD (); alert (retArr); // or return ret;} // Add the getYMD Method to the Date object to obtain the Date of the year, month, and day in the string format. prototype. getYMD = function () {var retDate = this. getFullYear () + "-"; // obtain the year. RetDate + = this. getMonth () + 1 + "-"; // obtain the month. RetDate + = this. getDate (); // get the day. Return retDate; // return date .} // Add the getDate Method to the String object so that the Date in String form is returned as a Date String. prototype. getDate = function () {var strArr = this. split ('-'); var date = new Date (strArr [0], strArr [1]-1, strArr [2]); return date ;} getDays ('2014-2-9 ', '2014-3-8'); </script>

Here, because the while LOOP condition is st. getTime ()! = Et. getTime (). Therefore, in the while loop, the period Date of the end date is not included in the result array, so the end date should be placed in the result array after the while period ends.


Method 3


<Script> // obtain the number of days interval function getDays (day1, day2) {// obtain the Date-type Date var st = day1.getDate () of the input parameter string format (); var et = day2.getDate (); var retArr = []; // obtain the year, month, and day of the start date. var yyyy = st. getFullYear (), mm = st. getMonth (), dd = st. getDate (); // loop while (st. getTime ()! = Et. getTime () {retArr. push (st. getYMD (); // use dd ++ to auto-increment the number of days st = new Date (yyyy, mm, dd ++ );} // Add the day of the end date to the array retArr. push (et. getYMD (); alert (retArr); // or return ret;} // Add the getYMD Method to the Date object to obtain the Date of the year, month, and day in the string format. prototype. getYMD = function () {// place the result in the array, use the join method of the array to return the connected string, and add zero return to less than two days and ten months. getFullYear (), fz (this. getMonth () + 1), fz (this. getDate ()]. join ("-");} // Add the getDate Method to the String object so that the Date in the String form is returned as a Date String. prototype. getDate = function () {var strArr = this. split ('-'); return new Date (strArr [0], strArr [1]-1, strArr [2]);} // month and day, add 0 function fz (num) {if (num <10) {num = "0" + num;} return num} getDays ('2017-2-9 ', '2014-3-8 '); </script>

The following two improvements are made in method 3:

  • Define the input parameter of new Date () in the while loop to the outside. You do not need to use st every time. getFullYear (), st. getMonth (), st. getDate () is obtained, and dd ++ can be directly used for the new Date () Day, which improves the computing efficiency.
  • In getYMD, directly use [this. getFullYear (), fz (this. getMonth () + 1), fz (this. getDate ()]. join ("-") replaces the previous four lines of code, and fills in the month and day. If there are less than two rows, add 0, in this way, the processed 8-bit character date can be saved to the database, which is also convenient for processing.



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.