JS Date processing Function--judge leap year, get total number of days in the month, add month

Source: Internet
Author: User

1. Determine if a leap year

function Isleapyear (eDate) {    var year = edate.getfullyear ();     return (((0 = = year% 4) && (0! = (year% 100))) | | (0 = = year%));}

2. Get the total number of days in a month

function Getdaysinmonth (eDate) {    var daysinmonth = [31,28,31,30,31,30,31,31,30,31,30,31];    daysinmonth[1] = Isleapyear (eDate)? 29:28;     return Daysinmonth[edate.getmonth ()];}

3. Add a few months to the date (if the date you get does not have this day, it will automatically become the last day of the month)

function addmonth (eDate, duration) {    var month = Edate.getmonth () + parseint (duration);     var day = edate.getdate ();      Edate.setdate (1);      Edate.setmonth (month);     var daysinmonth = getdaysinmonth (eDate);     if (Day > daysinmonth) {        = daysinmonth;    }    Edate.setdate (day);     return eDate;}

4. Date Modified

function changeexpirydate (expdate, Duration, durationtype) {    if(Durationtype = = 2) {//Day         expdate.setdate (expdate.getdate () +parseint (duration));    } Else if (Durationtype = = 3) {//Week        expdate.setdate (Expdate.getdate () +parseint (duration*7));    } Else if (Durationtype = = 4) {//Month        expdate = addmonth (Expdate.gettime (), duration);}    }

JS Date processing function--judging leap years, getting the total number of days in the month, adding a month

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.