javascript 添加天到日期格式 yyyy-mm-dd

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14842526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:43:16  来源:igfitidea点击:

Add days to date format yyyy-mm-dd

javascriptjquerydate

提问by user1632298

The question is simple, how can I add days to a date in YYYY-MM-DD format?

问题很简单,如何在 YYYY-MM-DD 格式的日期中添加天数?

For example, increment a date given in this format: "2013-02-11" to "2013-02-12"?

例如,将以这种格式给出的日期递增:“2013-02-11”到“2013-02-12”?

回答by Mallikarjuna Reddy

Hope below code will helpful to you

希望下面的代码对你有帮助

function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}

var myDate = new Date('2013-02-11');

var newDate = addDays(myDate,5);

回答by Sanjay Kamaruddin

date      = new Date('2013-02-11');
next_date = new Date(date.setDate(date.getDate() + 1));

here's a demo http://jsfiddle.net/MEptb/

这是一个演示http://jsfiddle.net/MEptb/

回答by Dumitru Chirutac

Something like this :

像这样的事情:

    var strDate = '2013-02-11'.split('-'),
        nrAddDays = 35,
        date = new Date(parseInt(strDate[0]), parseInt(strDate[1])-1, parseInt(strDate[2]));

    /* Add nr of days*/
    date.setDate(date.getDate() + nrAddDays);

    alert(date.toString());

I hope help you.

我希望能帮到你。