Home >Web Front-end >JS Tutorial >JavaScript method to get a specified date in the future

JavaScript method to get a specified date in the future

PHPz
PHPzforward
2016-05-16 19:24:391897browse

There is a date control in the requirement. The expected date of delivery can only be selected today and within 16 weeks in the future, so a verification is needed. The following is a piece of JS to get the specified date in the future

Get the future Specify the date YYYY-MM-DD n represents the number of days, such as 16 weeks, you need to convert the turnover into days before passing it in.

function getFutureDate(n) {
        var n = n;        var d = new Date();        var year = d.getFullYear();        var mon = d.getMonth() + 1;        var day = d.getDate();        if(day > n) {            if(mon > 1) {
                mon = mon + 1;
            } else {
                year = year + 1;
                mon = 12;
            }
        }
        d.setDate(d.getDate() + n);
        year = d.getFullYear();
        mon = d.getMonth() + 1;
        day = d.getDate();
        s = year + "-" + (mon < 10 ? (&#39;0&#39; + mon) : mon) + "-" + (day < 10 ? (&#39;0&#39; + day) : day);        return s;
    }

For more related tutorials, please visit JavaScript video tutorial

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete