JS gets middle all day from start and end time

Source: Internet
Author: User


Today in the forum see a post, given the input character type date and end of the character type date, calculate the intermediate interval for each day of the date. If the project is not busy, write it down and record it.


Original Problem

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

demand gets start and end time between all days return [' 2015-2-1 ', ' 2015-2-2 ' ... "2015-2-28", "2015-3-1"]


Method 1


<script>//Gets the interval number of days function getDays (day1, day2) {//Gets the date type date of the parameter string as a datetime var D1 = day1.getdate (); var d2 = Day2.getdate (  );//define the number of milliseconds in a day var daymilliseconds = 1000*60*60*24;//Gets the number of milliseconds to enter the date var d1ms = D1.gettime (); var d2ms = D2.gettime ();//define return value Var ret;//cycles the number of milliseconds until the d1ms is greater than or equal to d2ms when exiting the loop//end of each cycle, giving d1ms an additional day for (d1ms; d1ms <= d2ms; d1ms + = Daymilliseconds) {//IF RET is empty, you do not need to add "," as the delimiter if (!ret) {//will give the number of milliseconds to convert to a date date var day = new Date (d1ms);//gets its month and date form string ret = Day.getymd ();} else {//otherwise, give For each word of RET, add "," as delimiter var day = new Date (d1ms), ret = ret + ', ' + DAY.GETYMD ();}} Fuzhi alert (ret); Or can be changed to return ret;}  Add the Getymd method to the Date object to get the month-date Date.prototype.getYMD = function () {var retdate = this.getfullyear () + "-" in string form;    Gets the year.    Retdate + = This.getmonth () + 1 + "-";              Gets the month.               Retdate + = This.getdate ();    Get the day.                          return retdate; Returns the date. }//adds a GetDate method to a string object that returns a date in the form of a string as Date String.prototype.getDate = function () {var strarr = this.split ('-'); var Date = new Date (strarr[0], strarr[1]-1, strarr[2]); return date;} GetDays (' 2015-2-1 ', ' 2015-3-1 '); </script>


In the above method, the number of days of the month is calculated by using a comparison of the date milliseconds while the start date is self-increasing.

Getymd and GetDate are methods that are added to the date and String prototypes, respectively, to facilitate our use.



Method 2


Open the console console of the Chrome browser


you can see that the gettime of 32 days and April 1 are equal, so you can take advantage of this and let the days increment to complete the comparison.

<script>//Gets the interval number of days function getDays (day1, day2) {//Gets the date date of the parameter string as day var st = day1.getdate (); var et = Day2.getdate ( );//define the returned array var retarr = [];//loop, start date not equal to end date, loop while (St.gettime ()! = Et.gettime ()) {// Stores the date in the string form of the start date into the array Retarr.push (ST.GETYMD ());//Gets the start date of the day var tempdate = St.getdate ();//Starts the date St points to the new date constructed The new date is added one day more than the previous date St = new Date (St.getfullyear (), St.getmonth (), st.getdate () + 1);} Put the day of the end date into the array Retarr.push (ET.GETYMD ()); alert (Retarr); Or can be changed to return ret;}  Add the Getymd method to the Date object to get the month-date Date.prototype.getYMD = function () {var retdate = this.getfullyear () + "-" in string form;   Gets the year.    Retdate + = This.getmonth () + 1 + "-";             Gets the month.               Retdate + = This.getdate ();   Get the day.                          return retdate; Returns the date. }//adds a GetDate method to a string object that returns a date in the form of a string as Date String.prototype.getDate = function () {var strarr = this.split ('-'); var Date = new Date (strarr[0], strarr[1]-1, strarr[2]); return date;} GetDays (' 2015-2-9 ', ' 2015-3-8 '); </script>

Here because the condition of the while loop is st.gettime ()! = Et.gettime () So, the character date of the end date in the while loop is not put into the result array, so the end date is placed in the result array after the while end.


Method 3


<script>//Gets the interval number of days function getDays (day1, day2) {//Gets the date date of the parameter string as day var st = day1.getdate (); var et = Day2.getdate (  var Retarr = [];//gets start date of year, month, day var yyyy = St.getfullyear (), MM = St.getmonth (), DD = St.getdate ();//Loop while (St.gettime () ! = Et.gettime ()) {Retarr.push (St.getymd ());//The self-increment st = new Date (yyyy, MM, dd++) using dd++ for the number of days;} Put the day of the end date into the array Retarr.push (ET.GETYMD ()); alert (Retarr); Or can be changed to return ret;} Add the Getymd method to the Date object, get the month and day Date.prototype.getYMD = function () {///to put the result in the array, use the array's join method to return the concatenated string, and give less than two bits of day and month ten on 0 return [This.getfullyear (), FZ (This.getmonth () + 1), FZ (This.getdate ())].join ("-");} Add the GetDate method to the string object so that the date in the form of a string is returned as Date String.prototype.getDate = function () {var strarr = this.split ('-'); return new Date (Strarr[0], strarr[1]-1, strarr[2]);} For months and days, less than two bits of the front complement 0function FZ (num) {if (num <) {num = "0" + num;} Return num}getdays (' 2015-2-9 ', ' 2015-3-8 '); </script>

The following two improvements were made in Method 3

    • The entry for the new date () in the while loop is defined outside, not each time using st.getfullyear (), St.getmonth (), St.getdate ( ), and the new date () Days can be directly used dd++, improve the efficiency of the operation
    • GETYMD, directly using [This.getfullyear (), FZ (This.getmonth () + 1), FZ (This.getdate ())].join ("-") replaces the previous four lines of code, And to the month and day of the complement, in less than two bits when the front of 0, so that the processing of the 8-bit character date can save the database, and then take out the convenience of processing.



JS gets middle all day from start and end time

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.