You need to use getTime()
and setTime()
to add or substract the time in a javascript Date object. Using setDate()
and getDate()
will lead to errors when reaching the limits of the months 1, 30, 31, etc..
Using setTime allows you to set an offset in milliseconds, and let the Date object figure the rest:
var now=new Date();
var yesterdayMs = now.getTime() - 1000*60*60*24*1; // Offset by one day;
now.setTime( yesterdayMs );