Javascript 如何将天数添加到今天的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3818193/
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-08-23 06:16:44  来源:igfitidea点击:

How to add number of days to today's date?

javascriptjquerydate

提问by Linda725

I need to be able to add 1, 2 , 5 or 10 days to today's date using jQuery.

我需要能够使用 jQuery 将 1、2、5 或 10 天添加到今天的日期。

回答by p.campbell

You can use JavaScript, no jQuery required:

您可以使用 JavaScript,不需要 jQuery:

var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 

Formatting to dd/mm/yyyy:

格式化为dd/mm/yyyy

var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();

var someFormattedDate = dd + '/'+ mm + '/'+ y;

回答by Krishna Chytanya

You could extend the javascript Date object like this

您可以像这样扩展 javascript Date 对象

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(days));
    return this;
};

and in your javascript code you could call

在你的 javascript 代码中你可以调用

var currentDate = new Date();
// to add 4 days to current date
currentDate.addDays(4);

回答by user3945851

This is for 5 days:

这是5天:

var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));

You don't need JQuery, you can do it in JavaScript, Hope you get it.

你不需要 JQuery,你可以用 JavaScript 来完成,希望你明白。

回答by user3138856

Why not simply use

为什么不简单地使用

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

var newDate = addDays(new Date(), 5);

or -5 to remove 5 days

或 -5 删除 5 天

回答by Akash

Moment.js

时刻.js

Install moment.jsfrom here.

moment.js这里安装。

npm: $ npm i --save moment

纳米$ npm i --save moment

Bower: $ bower install --save moment

凉亭:$bower install --save moment

Next,

下一个,

var date = moment()
            .add(2,'d') //replace 2 with number of days you want to add
            .toDate(); //convert it to a Javascript Date Object if you like

Link Ref : http://momentjs.com/docs/#/manipulating/add/

链接参考:http: //momentjs.com/docs/#/manipulating/add/

Moment.jsis an amazing Javascript library to manage Date objects and extremely light weight at 40kb.

Moment.js是一个了不起的 Javascript 库,用于管理 Date 对象并且在40kb.

Good Luck.

祝你好运。

回答by Erik Aderhold

The prototype-solution from Krishna Chytanya is very nice, but needs a minor but important improvement. The days param must be parsed as Integer to avoid weird calculations when days is a String like "1". (I needed several hours to find out, what went wrong in my application.)

Krishna Chytanya 的原型解决方案非常好,但需要进行微小但重要的改进。days 参数必须被解析为 Integer 以避免当 days 是像“1”这样的字符串时进行奇怪的计算。(我需要几个小时才能找出我的应用程序出了什么问题。)

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(days));
    return this;
};

Even if you do not use this prototype function: Always be sure to have an Integer when using setDate().

即使你不使用这个原型函数: 在使用 setDate() 时总是确保有一个 Integer。

回答by Tomas ?ivickas

Pure JS solution, with date formatted YYYY-mm-ddformat

纯JS解决方案,带日期YYYY-mm-dd格式

var someDate = new Date('2014-05-14');
someDate.setDate(someDate.getDate() + 15); //number  of days to add, e.x. 15 days
var dateFormated = someDate.toISOString().substr(0,10);
console.log(dateFormated);

回答by Mulhoon

The accepted answer here gave me unpredictable results, sometimes weirdly adding months and years.

此处接受的答案给了我不可预测的结果,有时会奇怪地增加数月和数年。

The most reliable way I could find was found here Add days to Javascript Date object, and also increment month

我能找到的最可靠的方法是在这里找到 Add days to Javascript Date object, and also increment month

var dayOffset = 20;
var millisecondOffset = dayOffset * 24 * 60 * 60 * 1000;
december.setTime(december.getTime() + millisecondOffset); 

EDIT: Even though it has worked for some people, I don't think it is entirely correct. I would recommend going with a more popular answer or using something like http://momentjs.com/

编辑:尽管它对某些人有效,但我认为这并不完全正确。我建议使用更受欢迎的答案或使用类似http://momentjs.com/ 的内容



回答by Sahil J

If the times in the Date are not needed, then you can simply use the date object's methods to extract the Month,Year and Day and add "n" number of days to the Day part.

如果不需要 Date 中的时间,那么您可以简单地使用 date 对象的方法来提取 Month、Year 和 Day,并将“n”天数添加到 Day 部分。

var n=5; //number of days to add. 
var today=new Date(); //Today's Date
var requiredDate=new Date(today.getFullYear(),today.getMonth(),today.getDate()+n)

Ref:Mozilla Javascript GetDate

参考:Mozilla Javascript GetDate

Edit: Ref: Mozilla JavaScript Date

编辑:参考:Mozilla JavaScript 日期

回答by Eren Demir

Date.prototype.addDays = function(days)
{
    var dat = new Date(this.valueOf() + days * 24 * 60 * 60 * 1000 );
    return dat;
}