How to add days at a given time using JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
Tags add days
This article mainly introduces how to add days to a given time in JavaScript, involving related techniques related to date and time operations in JavaScript, for more information about how to add days in JavaScript at a given time, see the example in this article. We will share this with you for your reference. The details are as follows:

/*** Time addition processing function * @ param date the time to be calculated (xxxx-xx) * @ param plusDays the number of days to be added (integer) */function calcuDate (date, plusDays) {var dateArray = date. split ("-"); var year = + dateArray [0]; var month = + dateArray [1]; var day = + dateArray [2]; var oriDay = day; var secondMonthDays = (year % 4 = 0 & year % 100! = 0) | year % 400 = 0 )? 29: 28; while (plusDays> 0) {oriDay = day + = plusDays; switch (month) {case 4: case 6: case 9: case 11: if (day> 30) {plusDays-= (30-oriDay) + 1; month ++; day = 1 ;}else {plusDays = 0 ;}break; case 2: if (day> secondMonthDays) {plusDays-= (secondMonthDays-oriDay) + 1; month ++; day = 1;} else {plusDays = 0;} break; default: if (day> 31) {plusDays-= (31-oriDay) + 1; day = 1; month ++;} else {plusDays = 0 ;}} if (month> 12) {month = 1; year ++ ;}return createTimeString (year, month, day );}

Of course, in addition to this method, there are other solutions, such as passing parameters in new Date.

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.