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
Release: 2018-10-10 17:29:46
forward
1837 people have browsed it

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;
    }
Copy after login

For more related tutorials, please visit JavaScript video tutorial

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template