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 !
Gary Haran
October 1, 2007, October 01, 2007 10:41, permalink
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())
Dan Simard
October 1, 2007, October 01, 2007 11:28, permalink
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())
Gary Haran
October 1, 2007, October 01, 2007 11:47, permalink
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())
travis
October 1, 2007, October 01, 2007 11:55, permalink
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())
Sean Catchpole
October 1, 2007, October 01, 2007 21:41, permalink
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
hotel tuerkei
January 24, 2010, January 24, 2010 11:35, permalink
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
hotel Tuerkei vergleichen
February 23, 2010, February 23, 2010 04:44, permalink
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
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.