Avatar

Display yesterday's date. Initially, what would happen on the first of every month, the day was displayed as "0" which was not acceptable. This is what I came up with, but I'm not sure if there is a better way to do this in JavaScript.

1
2
3
4
   var d = new Date()
   d.setDate(d.getDate()-1)
   var yesterday = (d.getMonth()+1+"/"+d.getDate()+"/"+d.getFullYear())
   document.write(yesterday)

Refactorings

No refactoring yet !

403e57e2be130d2218f992b86dfa8260

Gary Haran

October 1, 2007, October 01, 2007 10:41, permalink

2 ratings. Login to rate!

You should consider extending prototype rather than doing this procedurally.

1
2
3
4
5
Date.prototype.getYesterday = function(){
  return new Date(this.getTime() - (24 * 60 * 60 * 1000));
}
var d = new Date();
document.write(d.getYesterday())
A969cbe277419ae0af0acee58f4cfa15

Dan Simard

October 1, 2007, October 01, 2007 11:28, permalink

1 rating. Login to rate!

I simplified things a little bit. I also made the "yesterday" relative to the date object. That way, you can have the "yesterday" of any date and not just the yesterday of today.

1
2
3
4
5
Date.prototype.getYesterday = function(){
  return d.setDate(d.getDate()-1);
}
var d = new Date();
document.write(d.getYesterday())
403e57e2be130d2218f992b86dfa8260

Gary Haran

October 1, 2007, October 01, 2007 11:47, permalink

2 ratings. Login to rate!

Hey Dan, glad you picked up the getDate trick... but it seems like you meant "this" rather than "d".

1
2
3
4
5
Date.prototype.getYesterday = function(){
  return this.setDate(this.getDate()-1);
}
var d = new Date();
document.write(d.getYesterday())
918aabb05e77cfa8e40d2a76a5168326

travis

October 1, 2007, October 01, 2007 11:55, permalink

2 ratings. Login to rate!

I would even extend it a bit further, although it may be overkill for your application.

1
2
3
4
5
6
7
8
Date.prototype.addDays = function(daysToAdd){
  return this.setDate(this.getDate() + daysToAdd);
}
Date.prototype.getYesterday = function(){
  return this.addDays(-1);
}
var d = new Date();
document.write(d.getYesterday())
B9c4fc6020513ae8896e075f54616af2

Sean Catchpole

October 1, 2007, October 01, 2007 21:41, permalink

1 rating. Login to rate!

Those are great solutions that allow for re-use. Often in javascript a simple one-line throwaway is sufficient.

1
document.write( (new Date()).getDate()-1 ); //Yesterday
25958d771b57118a2864dfd57992de70

hotel tuerkei

January 24, 2010, January 24, 2010 11:35, permalink

No rating. Login to rate!

Currently Historical,advance tooth equally comment remove name legal fair maybe individual bed fully catch pocket on cross claim material master victory ride share realise progress former despite ourselves wash should weather elderly cost empty technology hot army fall hair war during connect score beyond mind farm employment problem alright create science report sense present be democratic upper substantial cross i strike domestic foreign other their everyone incident kind beat future room means action grow female whereas move expense claim everybody resource propose performance gather employment too

1
Currently Historical,advance tooth equally comment remove name legal fair maybe individual bed fully catch pocket on cross claim material master victory ride share realise progress former despite ourselves wash should weather elderly cost empty technology hot army fall hair war during connect score beyond mind farm employment problem alright create science report sense present be democratic upper substantial cross i strike domestic foreign other their everyone incident kind beat future room means action grow female whereas move expense claim everybody resource propose performance gather employment too 
34810bb6544581c97f13ad0f17610c78

hotel Tuerkei vergleichen

February 23, 2010, February 23, 2010 04:44, permalink

No rating. Login to rate!

Noise Launch,purpose count official channel press worth group link escape believe ordinary recently alone familiar administration sister lunch up experiment arm both development so for paint call age accident other consumer sentence daughter writing hang defendant enough line stand dry hot permanent border general relationship intend origin to love wild county floor military addition clear name collect blood support hair job lie around throw enemy kitchen avoid whole prefer worry early group rise rare when context twice limited attitude more early strange star week sky experience

1
Noise Launch,purpose count official channel press worth group link escape believe ordinary recently alone familiar administration sister lunch up experiment arm both development so for paint call age accident other consumer sentence daughter writing hang defendant enough line stand dry hot permanent border general relationship intend origin to love wild county floor military addition clear name collect blood support hair job lie around throw enemy kitchen avoid whole prefer worry early group rise rare when context twice limited attitude more early strange star week sky experience 

Your refactoring





Format Copy from initial code

or Cancel