How to use JavaScript time to process specified dates a few months ago or a few months later

Source: Internet
Author: User
During Normal project development, there are often two cases of processing time in JavaScript (1, logic processing 2, Format Conversion Processing ). Of course, I would like to talk about related technologies, and I can catch them with my eyes closed in the garden, but what I want to do is: Since I am lucky enough to be met, we need to try our best to translate the analysis into something that best suits me, be a part of your own knowledge base, and hope to help users who need to process time in JavaScript during normal project development, logic processing 2, Format Conversion Processing ). Of course, I would like to talk about related technologies, and I can catch them with my eyes closed in the garden, but what I want to do is: Since I am lucky enough to be met, we need to try our best to translate the analysis into something that best suits me, become part of your knowledge base, and hope to help those who need it to solve the problem.


Time Logic Processing

The common requirement for this type is to calculate the date of today after a few months (Before.

/*** Get input date a few months ago * {param: DateTime} date input date (YYYY-MM-DD) * {param: number} monthNum number of months */GetPreMonthDay: function (date, monthNum) {var dateArr = date. split ('-'); var year = dateArr [0]; // obtain the year of the current date var month = dateArr [1]; // obtain the current Date's month var day = dateArr [2]; // obtain the current Date's day var days = new Date (year, month, 0); days = days. getDate (); // obtain the number of days in the month of the current date var year2 = year; var mon2= parseInt (month)-monthNum; I F (mon2's <= 0) {year2 = parseInt (year2)-parseInt (mon2's/12 = 0? 1: parseInt (mon2)/12); mon2= 12-(Math. abs (mon2) % 12);} var day2 = day; var days2 = new Date (year2, mon2, 0); days2 = days2.getDate (); if (day2> days2) {day2 = days2;} if (mon2's <10) {mon2's = '0' + mon2's;} var t2 = year2 + '-' + mon2's + '-' + day2; return t2 ;}


/*** Get input date for next month * {param: DateTime} date input date (YYYY-MM-DD) * {param: number} monthNum number of months */GetNextMonthDay: function (date, monthNum) {var dateArr = date. split ('-'); var year = dateArr [0]; // obtain the year of the current date var month = dateArr [1]; // obtain the current Date's month var day = dateArr [2]; // obtain the current Date's day var days = new Date (year, month, 0); days = days. getDate (); // obtain the number of days of the month in the current date var year2 = year; var mon2= parseInt (month) + parseInt (MonthNum); if (monty2> 12) {year2 = parseInt (year2) + parseInt (monty2)/12 = 0? 1: parseInt (mon2)/12); mon2= parseInt (mon2) % 12;} var day2 = day; var days2 = new Date (year2, mon2, 0 ); days2 = days2.getDate (); if (day2> days2) {day2 = days2;} if (mon22. <10) {mon2's = '0' + mon22 ;} var t2 = year2 + '-' + mon2+ '-' + day2; return t2 ;}

Test results:

-->

The above is to teach you how to use JavaScript time to process detailed content of the specified date a few months ago or a few months later. For more information, see other related articles in the first PHP community!

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.