Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get today's, tomorrow's and yesterday's dates on javascript [duplicate]

I'm trying to get the current date and tomorrow's date and yesteday's date as well in this format 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.

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;        
}
like image 213
Yasser B. Avatar asked May 22 '15 12:05

Yasser B.


Video Answer


1 Answers

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....

var yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
like image 109
Jordi Castilla Avatar answered Oct 14 '22 10:10

Jordi Castilla