JavaScript如何以格式dd-mm-yy获取明天的日期

daz*_*166 77 javascript

我试图让JavaScript以格式显示明天的日期(dd-mm-yyyy)

我有这个脚本以格式显示今天的日期(dd-mm-yyyy)

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" + day + "/" + month + "/" + year + "</b>")

Displays: 25/2/2012 (todays date of this post)
Run Code Online (Sandbox Code Playgroud)

但是如何让它以相同的格式显示明天日期,即 26/2/2012

我试过这个:

var day = currentDate.getDate() + 1
Run Code Online (Sandbox Code Playgroud)

但是我可以保持+1并且超过31,显然一个月没有> 32天

一直在寻找几个小时,但似乎没有答案或解决方案?

Rod*_*ist 156

这应该对你来说真的很好.

如果你传递Date构造函数,它将完成其余的工作.

24小时60分60秒1000毫秒

var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" + day + "/" + month + "/" + year + "</b>")
Run Code Online (Sandbox Code Playgroud)

有一点要记住的是,这种方法会返回日期从现在整整24个小时,这可能是围绕夏令时不准确的.

Phil的回答工作随时都有:

var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 1);
Run Code Online (Sandbox Code Playgroud)

我编辑帖子的原因是因为我自己创建了一个在DST期间使用旧方法曝光的错误.

  • 请注意,使用此策略,您可能会遇到DST问题,导致一年中的某一天有23个小时而一个有25个.菲尔的答案可以避免这个问题. (12认同)
  • 谢谢,更短一些`var currentDate = new Date(+ new Date()+ 86400000);` (8认同)
  • `new Date().getTime()`可以简化为`Date.now()` (2认同)
  • @ConorB, .getMonth() 1 月返回 0,2 月返回 1 ...... 12 月返回 11。添加 1 将其从数组索引变为人类可读的日期。 (2认同)

Phi*_*hil 122

JavaScript Date类为您处理此问题

var d = new Date("2012-02-29")
console.log(d)
// Wed Feb 29 2012 11:00:00 GMT+1100 (EST)

d.setDate(d.getDate() + 1)
console.log(d)
// Thu Mar 01 2012 11:00:00 GMT+1100 (EST)

console.log(d.getDate())
// 1
Run Code Online (Sandbox Code Playgroud)

  • @Timo非常确定我的例子正是如此 (17认同)

Isu*_*han 8

仅使用JS(纯js)

今天

new Date()
//Tue Oct 06 2020 12:34:29 GMT+0530 (India Standard Time)
new Date(new Date().setHours(0, 0, 0, 0))
//Tue Oct 06 2020 00:00:00 GMT+0530 (India Standard Time)
new Date(new Date().setHours(0, 0, 0,0)).toLocaleDateString('fr-CA')
//"2020-10-06"
Run Code Online (Sandbox Code Playgroud)

明天

new Date(+new Date() + 86400000);
//Wed Oct 07 2020 12:44:02 GMT+0530 (India Standard Time)
new Date(+new Date().setHours(0, 0, 0, 0) + 86400000);
//Wed Oct 07 2020 00:00:00 GMT+0530 (India Standard Time)
new Date(+new Date().setHours(0, 0, 0,0)+ 86400000).toLocaleDateString('fr-CA')
//"2020-10-07"
//don't forget the '+' before new Date()
Run Code Online (Sandbox Code Playgroud)

后天

只需乘以二即可,例如:- 2*86400000

您可以从/sf/answers/223421061/找到所有区域设置短代码


Mat*_*ttW 6

我会使用DateJS库.它可以做到这一点.

http://www.datejs.com/

执行以下操作:

var d = new Date.today().addDays(1).toString("dd-mm-yyyy");
Run Code Online (Sandbox Code Playgroud)

Date.today() - 今天午夜送你.

  • Date.parse( '明天')的toString( 'DD-MM-YYYY'); (4认同)

cze*_*rny 5

方法Date.prototype.setDate()接受标准范围之外的偶数参数,并相应地更改日期.

function getTomorrow() {
    const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1); // even 32 is acceptable
    return `${tomorrow.getFullYear()}/${tomorrow.getMonth() + 1}/${tomorrow.getDate()}`;
}
Run Code Online (Sandbox Code Playgroud)