JavaScript---Date (): Sort by line of rotation

Source: Internet
Author: User

These days, the master began to do a project, to give a number of financial institutions weekly row value. According to the background code of the project, the master told me to use JavaScript to write the front-shift scheduling code, to achieve four rotating lines on the weekly cycle duty function. Here is the code, in order to resolve the referenced problem, the code is a bit wordy. Although solve the problem, feel should be able to have a simpler implementation method, but now can not think of a better method ~~~~~ hope everyone to give a suggestion, thank you!

1     varBank =["ICBC", "abc", "BoC", "CCB"],2result = [];3 4     varBegindate=NewDate ("2015/02/16"),5Enddate=NewDate ("2016/02/21"),6SECONDS=24 * 60 * 60 * 1000,7 8            //calculates the total number of days between the specified start date and the end date9Day= (enddate-begindate)/seconds,Ten  One            //calculates the total number of weeks between the specified start date and the end date ADiffer= (day+1)/7;

First, an array is created, the row name that needs to be rotated is written into the array, and the new date () is used to create the start date and end date of the row value;

The created date returns the number of milliseconds, the ending date and the start date minus the number of days between the specified date, and the total number of weeks in addition to 7;

After that you need to cycle to get the Order of Duty:

1varBegin_day =begindate;2 3      for(vari = 0; I <differ;) {4 5         //nested loops, which can be sorted by the rotation line6          for(varj = 0; J < Bank.length; J + +) {7 8             ///once againAfter you create a new Date (), End_day and Begin_day have different pointers9             //So changing the value of End_day does not cause the value of Begin_day to be changedTen             varEnd_day =NewDate (Begin_day.getfullyear () + "-" + One(Begin_day.getmonth () + 1) + "-" +begin_day.getdate ()); A  -             //Reset the value of the End_day,date increased by 6 to date Sunday -End_day.setdate (End_day.getdate () + 6); the  -             vartemp = [Begin_day, End_day, chinesedatestring (begin_day) + "---" + - chinesedatestring (End_day), Bank[j]]; -             if(I > Differ-1) { +                  Break; -             } +  A             ///once againafter creating the new Date (), Temp_day and Begin_day also have different pointers
at varTemp_day =NewDate (Begin_day.getfullyear () + "-" + -(Begin_day.getmonth () + 1) + "-" +begin_day.getdate ()); - - //Reset the value of the Temp_day by 7 to get the date of the next Monday -Temp_day.setdate (Temp_day.getdate () + 7); - -Begin_day =Temp_day; to +i++; - Result.push (temp); the } * } $ Panax Notoginseng functionchinesedatestring (date){ return date.getfullyear () + "year" + (Date.getmonth () + 1) + "month" + date.ge Tdate () + "Day"; +}

The first two locations of the newly created temp array are written directly without creating the Date () function at the beginning of the code:


The value of Begin_day is found to vary with the value of Begin_day.setdate (Begin_day.getdate () +6).
Create a date (), save Begin_day and then make changes, the two have different pointers, and will not be affected by each other;
Again,getMonth () + 1, in order to get the actual month;


Assigns the reset Temp_day value to the begin_day so that the value of begin_day is changed each time the loop is executed;

The last chinesedatestring (date) function returns the date format you want to output!

Although solve the reference problem, but think the code is somewhat complex, there is a more concise or better way ~ ~ I hope you give a lot of advice!!

(Code parsing is a bit messy, I hope you understand the understanding ~ ~ ~ ^^)

JavaScript---Date (): Sort by line of rotation

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.