在 javascript 中添加一天到日期

我相信很多人都问过这个问题,但是当我检查答案的时候,在我看来,我发现的答案是错误的

var startDate = new Date(Date.parse(startdate));
//The start date is right lets say it is 'Mon Jun 30 2014 00:00:00'


var endDate = new Date(startDate.getDate() + 1);
// the enddate in the console will be 'Wed Dec 31 1969 18:00:00' and that's wrong it should be  1 july

我知道 .getDate()从1-31返回,但是浏览器或 javascript 是否只增加了一天而没有更新月份和年份?

在这种情况下,我应该写一个算法来处理这个问题吗? 还是有其他的方法?

172692 次浏览

请注意,Date.getDate只返回月中的某一天。您可以通过调用 Date.setDate并附加1来添加一天。

// Create new Date instance
var date = new Date()


// Add a day
date.setDate(date.getDate() + 1)

JavaScript 将自动为您更新月份和年份。

编辑:
Here's a link to a page where you can find all the cool stuff about the built-in Date object, and see what's possible: 日期.

我觉得你要找的是:

startDate.setDate(startDate.getDate() + 1);

还有,你可以看看 等一下

用于解析、验证、操作和格式化日期的 javascript 日期库。

If you don't mind using a library, DateJS (https://github.com/abritinthebay/datejs/) would make this fairly easy. You would probably be better off with one of the answers using vanilla JavaScript however, unless you're going to take advantage of some other DateJS features like parsing of unusually-formatted dates.

如果你正在使用 DateJS,这样的一行应该可以解决这个问题:

Date.parse(startdate).add(1).days();

你也可以使用 MomentJS,它有类似的特性(http://momentjs.com/) ,但是我对它不太熟悉。

用这个我认为对你有用

var endDate=startDate.setDate(startDate.getDate() + 1);

接受单个数字的 Date构造函数期望的是从 1969年12月31日开始的毫秒数。

Date.getDate()返回当前日期对象的日索引。在您的示例中,日期是 30。最后一个表达式是 31,因此它在 1969年12月31日之后返回31毫秒。

使用现有方法的一个简单解决方案是改用 Date.getTime()。然后,添加一天值的毫秒而不是 1

比如说,

var dateString = 'Mon Jun 30 2014 00:00:00';


var startDate = new Date(dateString);


// seconds * minutes * hours * milliseconds = 1 day
var day = 60 * 60 * 24 * 1000;


var endDate = new Date(startDate.getTime() + day);

JSFiddle

请注意,此解决方案不处理与夏令时、闰年等有关的边缘情况。相反,使用 Moment.js 这样的成熟开源库来处理所有事情总是一种更具成本效益的方法。

There is issue of 31st and 28th Feb with getDate() I use this function getTime and 24*60*60*1000 = 86400000

使用此功能:


function incrementDate(dateInput,increment) {
var dateFormatTotime = new Date(dateInput);
var increasedDate = new Date(dateFormatTotime.getTime() +(increment *86400000));
return increasedDate;
}

例如:

var dateWith31 = new Date("2017-08-31");
var dateWith29 = new Date("2016-02-29");


var amountToIncreaseWith = 1; //Edit this number to required input


console.log(incrementDate(dateWith31,amountToIncreaseWith));
console.log(incrementDate(dateWith29,amountToIncreaseWith));


function incrementDate(dateInput,increment) {
var dateFormatTotime = new Date(dateInput);
var increasedDate = new Date(dateFormatTotime.getTime() +(increment *86400000));
return increasedDate;
}

var datatoday = new Date();
var datatodays = datatoday.setDate(new Date(datatoday).getDate() + 1);
todate = new Date(datatodays);
console.log(todate);

这会帮助你..。

只是为了向 Date原型添加功能:

以一种易变的方式/风格:

Date.prototype.addDays = function(n) {
this.setDate(this.getDate() + n);
};


// Can call it tomorrow if you want
Date.prototype.nextDay = function() {
this.addDays(1);
};


Date.prototype.addMonths = function(n) {
this.setMonth(this.getMonth() + n);
};


Date.prototype.addYears = function(n) {
this.setFullYear(this.getFullYear() + n);
}


// etc...


var currentDate = new Date();
currentDate.nextDay();

我知道这已经是很久以前的事了,但是这是我的答案

function addDays(date, n)
{
const oneDayInMs = 86400 * 1000;
return new Date(Date.parse(date) + (n * oneDayInMs));
}
addDays(new Date(), 1);

在一行中添加一天的 javascript

注意: 如果你想添加一个特定的天数... 只需用你想要的天数替换1


new Date(new Date().setDate(new Date().getDate() + 1))

console.log(new Date(new Date().setDate(new Date().getDate() + 1)))

下面将增加一天到当前时间。我相信这将处理夏令时等。

function increment_date (date) {
let old_date = new Date (date);
date.setDate (old_date.getDate() + 1);
while (date.getDate() == old_date.getDate()) {
date.setHours (date.getHours() + 1);
}
date.setHours (0);
}