9

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;        
}
Yasser B.
  • 835
  • 6
  • 20
  • 34
  • If I am not wrong, you want someone to review your code and suggest best approaches. If that's so, you need to post your question on codereview.stackexchange.com. – Harish May 22 '15 at 12:58
  • Before this got marked as a duplicate, I wrote a quick little script. View this fiddle if you want. It allows you to set the amount of days forward and backward you want to go. http://jsfiddle.net/ctwheels/94out9dx/ – ctwheels May 22 '15 at 13:20
  • Also, as an added note, although the marked as duplicate post deals with adding dates to the current date, @YasserB. 's question deals with adding and removing dates and the question was not how to do so, but how to improve upon it if there was a better method. These are clearly 2 different questions that simply overlap in ideas – ctwheels May 22 '15 at 13:23
  • @ctwheels Thanks for your comments and for your Jsfiddle code, it really helps, I just wanted to make sure that my code works fine, and I want to undestand more how we can play with dates in Javascript. thanks again – Yasser B. May 22 '15 at 13:30
  • @YasserB. Just to let you know, I updated my code to fix some issues. The code now works in terms of time instead of date (prevents negative dates from showing up) and also adds a leading zero if the date's number is less than 10 – ctwheels May 22 '15 at 15:16

1 Answers1

14

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);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • I think this is far too complex. Something you'll look back on and say: "what does this code do?" – Steven Lemmens May 22 '15 at 12:54
  • 1
    @Steven because of that's why programmers do we have variable names and comments... – Jordi Castilla May 22 '15 at 14:00
  • 5
    True, but why go the way of detracting 24 hours, and multiplying by 60, 60, 60 and 1000 if you can simply do d.getDate() - 1 ? This is maybe something to do with personal preference. Anyways, I was just commenting. I didn't downvote your suggestion or anything because it clearly will work. – Steven Lemmens May 22 '15 at 15:14
  • "I think this is far too complex." Go and have a look at how painful it is to write this simple code in Xcode... Now that's a masterclass in "complex" !! ;-) – Mike Gledhill Jan 24 '17 at 09:25