Detailed description of the date specified by the JavaScript time processing several months ago or a few months later, detailed description of javascript

Source: Internet
Author: User

Detailed description of the date specified by the JavaScript time processing several months ago or a few months later, detailed description of javascript

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, 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:

I have not found a problem. I pushed it on the 29th, and it will be 29 before and after. The period from January 1, to January 1, (because January 29 ).

This logic needs to be changed according to the actual situation for some requirements. For example, I want to pay the deposit fee for one month. Currently, I have paid 2.1-2.29 (the whole month of July February 1), and the monthly deposit should be (3.1-3.31 ). The logic is 3.1-3.29. Therefore, use it based on the actual situation!

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.