Detailed JavaScript time processing a few months ago or months after the specified date _javascript tips

Source: Internet
Author: User
Tags getdate knowledge base

In common project development process, often encounter needs in JavaScript processing time, nothing but two (1, Logic processing 2, Format conversion processing). Of course, the relevant technology blog, the garden is closed with eyes can catch one, but I have to do is: since I have been fortunate to encounter, we should be as far as possible analysis into their best for their own things, become a part of their knowledge base, and hope to help the needy students to solve the problems encountered in the relevant small.

Time Logic Processing

Common requirements for this type are: To calculate today's date for a few months (before).

/** * Get the input date of a few months ago *{param:datetime} Date Input date (YYYY-MM-DD) *{param:number} monthnum months
  * * Getpremonthday:function (date,monthnum) {var Datearr = date.split ('-'); var year = datearr[0]; Gets the year of the current date var month = datearr[1]; Gets the current date's month var day = datearr[2];
  Gets the day var days of the current date = new Date (year, month, 0); Days = Days.getdate ();
  Gets the number of days in the current date of the month var year2 = year;
  var month2 = parseint (month)-monthnum;
   if (Month2 <=0) {year2 = parseint (year2)-parseint (MONTH2/12 = 0? 1:parseint (month2)/12);
  Month2 =-(Math.Abs (month2)% 12);
  var day2 = day;
  var days2 = new Date (year2, month2, 0);
  Days2 = Days2.getdate ();
  if (Day2 > Days2) {day2 = Days2;
  } if (Month2 <) {month2 = ' 0 ' + month2;
  var t2 = year2 + '-' + month2 + '-' + day2;
 return T2; }
 /**
 * Get next one months input date
 *{param:datetime} date Input date (YYYY-MM-DD)
 *{param:number} monthnum months
 * * Getnextmonthday:function (date, monthnum)
 {
  var Datearr = date.split ('-');
  var year = datearr[0]; Gets the year of the current date
  var month = datearr[1];//Get the current date's month
  var day = datearr[2];//Get the current date
  var days = new Date (years, M Onth, 0);
  Days = Days.getdate (); Gets the number of days of the month in the current date
  var year2 = year;
  var month2 = parseint (month) + parseint (monthnum);
  if (Month2 >12) {
   year2 = parseint (year2) + parseint (parseint (month2)/= 0 1:parseint (month2)/()) 
   month2 = parseint (month2)%
  }
  var day2 = day;
  var days2 = new Date (year2, month2, 0);
  Days2 = Days2.getdate ();
  if (Day2 > Days2) {
   day2 = days2;
  }
  if (Month2 <) {
   month2 = ' 0 ' + month2;
  }

  var t2 = year2 + '-' + month2 + '-' + day2;
  return t2;
 }

Test effect:

The hair did not find a problem, through 29th to push, before and after will be 29. January 31 to February is February 29 (because February is 29 days).

Then this logic has to be changed according to the actual situation for some requirements. For example: I would like to pay a deposit of one months. At present I have paid the 2.1-2.29 (February month), the deposit one months should be (3.1-3.31). Through this logic is 3.1-3.29, so use please according to the actual situation!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat 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.