如何在JavaScript上获取今天,明天和昨天的日期 [英] How to get today's, tomorrow's and yesterday's dates on javascript

查看:81
本文介绍了如何在JavaScript上获取今天,明天和昨天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以这种格式获取当前日期,明天日期和昨天日期 2015-05-21

I'm trying to get the current date and tomorrow's date and yesteday's date as well in this format 2015-05-21

我已经使这些功能正常工作,但是我想请您谅解,我不是javascript方面的专家,所以我想确保它可以在我的应用程序中正常工作。 / p>

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.

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;        
}


推荐答案

今天的

var currentDate = new Date();

打印:

var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()

然后...如果一天有24小时>> 60分钟60秒1000毫秒....

Then... if a day has 24 hours >> 60 minutes 60 seconds 1000 milliseconds....

var yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);

这篇关于如何在JavaScript上获取今天,明天和昨天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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