JS date, Month: Date plus one Tian Deng

Source: Internet
Author: User

//date, based on the original date, increase days, by default 1 days        functionadddate (date, days) {if(days = = Undefined | | days = = ") { days= 1; }            varDate =Newdate (date); Date.setdate (Date.getdate ()+Days ); varmonth = Date.getmonth () + 1; varDay =date.getdate (); returnDate.getfullyear () + '-' + getformatdate (month) + '-' +getformatdate (day); }        //Date Month/day display, if 1 digits, precede with ' 0 '        functionGetformatdate (ARG) {if(arg = = Undefined | | arg = = ") {                return‘‘; }            varRe = arg + '; if(Re.length < 2) {Re= ' 0 ' +re; }            returnre; }

Reference:

Title, began to check the use of JS files, but did not find the function can be directly used, so I would like to write the function, which involves the judgment of the number of days per month, if it is February, but also related to the judgment of leap years, although not complex but I think JS should not be so low, so looked up the following information, Finally, the following significant discovery, in order to increase the number of days on a date, in fact, as long as the call Date object's Setdate () function can be, the method is as follows:
function Adddate (date,days) {
var d=new date (date);
D.setdate (D.getdate () +days);
var m=d.getmonth () +1;
return d.getfullyear () + '-' +m+ '-' +d.getdate ();
}
Where the date parameter is to be added and minus the day, the days parameter is to be added and minus the number, if the forward count is passed in negative numbers, the next count is passed in positive numbers, if the month is to be added and minus, call Setmonth () and getmonth () on it, It is important to note that the returned month is calculated starting at 0, which means that the returned month is one months less than the actual month, so add 1 accordingly.

JS date, Month: Date plus one Tian Deng

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.