javascriptdate

Calculate the date yesterday in JavaScript


How can I calculate yesterday as a date in JavaScript?


Solution

  • var date = new Date();
    
    console.log(date); //# => Fri Apr 01 2011 11:14:50 GMT+0200 (CEST)
    
    date.setDate(date.getDate() - 1);
    
    console.log(date); //# => Thu Mar 31 2011 11:14:50 GMT+0200 (CEST)