Javascript 如何在javascript上获取今天、明天和昨天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30397156/
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
How to get today's, tomorrow's and yesterday's dates on javascript
提问by Yasser B.
I'm trying to get the current date and tomorrow's date and yesteday's date as well in this format 2015-05-21
我正在尝试以这种格式获取当前日期和明天的日期以及昨天的日期 2015-05-21
I've made these functions which work pretty good, but I wanna get your perspective about that, i'm not really expert in javascript, so I want to make sure that will work properly on my application.
我已经使这些功能运行良好,但我想了解您对此的看法,我不是 javascript 方面的专家,所以我想确保它们能在我的应用程序上正常运行。
function gettodaydate() {
var d = new Date();
var date=""+d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate();
return date;
}
function gettomorrowdate() {
var d = new Date();
var date=""+d.getFullYear()+"-"+d.getMonth()+"-"+(d.getDate()+1);
return date;
}
function getyesterdaydate() {
var d = new Date();
var date=""+d.getFullYear()+"-"+d.getMonth()+"-"+(d.getDate()-1);
return date;
}
回答by Jordi Castilla
Today's
今天的
var currentDate = new Date();
Print:
打印:
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
Then... if a day has 24 hours >> 60 minutes 60 seconds 1000 milliseconds....
那么...如果一天有 24 小时 >> 60 分 60 秒 1000 毫秒....
var yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);

