JS gets a collection of historical or future months based on the current month

Source: Internet
Author: User

Starting from the current month, when the previous month is used, the calculation factor is 12. When the value is reduced by 1 and 0, the year is reduced by 1, the month is restored to 12, and then pushed back;

Starting from the current month, when the next month is used, the calculation factor is 12. If the value is 1 and the value is 13, the year is added to 1, the month is restored to 12, and then pushed forward.

<SCRIPT type = "text/JavaScript"> // gets the collection of historical months based on the current month (from the current month before the push: History) function complementhistorydate (nummonth) {var compldate = []; var curdate = new date (); var y = curdate. getfullyear (); var M = curdate. getmonth () + 1; // load the current month for the first time (Format: yyyy-mm) compldate [0] = Y + "-" + (M. tostring (). length = 1? "0" + M: m); m --; // It has been loaded for the first time. nummonth is calculated once less than once for (VAR I = 1; I <nummonth; I ++, m --) {If (M = 0) {// one year later, y --; M = 12; // push back from March 13, December} compldate [I] = Y + "-" + (M. tostring (). length = 1? "0" + M: m);} return compldate;} // obtain the collection of future months based on the current month (from the current month to the future) function complementfuturedate (nummonth) {var compldate = []; var curdate = new date (); var y = curdate. getfullyear (); var M = curdate. getmonth () + 1; // load the current month for the first time (Format: yyyy-mm) compldate [0] = Y + "-" + (M. tostring (). length = 1? "0" + M: m); m ++; // It has been loaded for the first time. nummonth is calculated once less than once for (VAR I = 1; I <nummonth; I ++, M ++) {If (M = 13) {// one year later than January 1, December, y ++; M = 1; // push forward from March 13, January} compldate [I] = Y + "-" + (M. tostring (). length = 1? "0" + M: m);} return compldate ;}</SCRIPT>

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.