在javascript中添加一天到目前为止 [英] Add one day to date in javascript
问题描述
我确信很多人都会问这个问题,但当我查看答案时,在我看来,我发现的错误是错误的
I am sure that a lot of people asked this question but when I checked the answers it seems to me that they are wrong that what I found
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只增加了一天而没有更新一个月和一年?
I know that .getDate()
return from 1-31 but Does the browser or the javascript increase only the day without updating the month and the year ?
在这种情况下我应该写一个算法来处理这个问题吗?还是有另一种方法?
and in this case Should I write an algorithm to handle this ? or there is another way ?
推荐答案
请注意 Date.getDate
返回该月的某一天。您可以通过调用 Date.setDate
并附加1来添加一天。
Note that Date.getDate
only returns the day of the month. You can add a day by calling Date.setDate
and appending 1.
// Create new Date instance
var date = new Date()
// Add a day
date.setDate(date.getDate() + 1)
JavaScript会自动为您更新月份和年份。
JavaScript will automatically update the month and year for you.
编辑:
这是一个指向页面的链接,您可以在其中找到有关内置Date对象的所有精彩内容,并查看可能的内容:日期。
这篇关于在javascript中添加一天到目前为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!