JavaScript如何以格式dd-mm-yy获取明天的日期 [英] JavaScript how to get tomorrows date in format dd-mm-yy

查看:120
本文介绍了JavaScript如何以格式dd-mm-yy获取明天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让JavaScript以格式显示明天日期(dd-mm-yyyy)

I am trying to get JavaScript to display tomorrows date in format (dd-mm-yyyy)

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

I have got this script which displays todays date in format (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)

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

But how do I get it to display tomorrows date in the same format i.e. 26/2/2012

我试过这个:

var day = currentDate.getDate() + 1

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

However I could keep +1 and go over 31 obviously there are not >32 days in a month

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

Been searching for hours but seems to be no answer or solution around this?

推荐答案

这应该修复它真的很适合你。

This should fix it up real nice for you.

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

If you pass the Date constructor a time it will do the rest of the work.

24小时60分60秒1000毫秒

24 hours 60 minutes 60 seconds 1000 milliseconds

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>")

要记住的一件事是,此方法将在24小时后返回日期,这可能在夏令时期间不准确。

One thing to keep in mind is that this method will return the date exactly 24 hours from now, which can be inaccurate around daylight savings time.

菲尔的回答随时工作:

var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 1);

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

The reason I edited my post is because I myself created a bug which came to light during DST using my old method.

这篇关于JavaScript如何以格式dd-mm-yy获取明天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆