Dates are a peculiar type of data that we have to work with. In some sense, they aren’t a number, but instead are a combination of month, day, and year. But at the same time, in most programming languages, they are fundamentally a number: the number of seconds since the Unix epoch.
Adding to Dates
I first wanted to move a date forward a week, so I thought that perhaps I could add 7 days worth of milliseconds to the date to get next week. Go ahead and try it:
new Date() + (1000 * 60 * 60 * 24 * 7)
How To Add
In order to add to Dates, I had to create a new Date, using the value returned from getTime and adding milliseconds to it, and using that new (humongous) number in the constructor.
var nextWeek = new Date(today.getTime() + 1000 * 60 * 60 * 24 * 7);
This is because the Date’s toString method is being evaluated instead of it’s valueOf. We can also overcome that with an additional plus sign.
+(new Date()) + (1000 \* 60 \* 60 \* 24 \* 7)
Subtracting from Dates
new Date() - (1000 * 60 * 60 * 24 * 7)
Result is a number: 1252714884521. This does the proper math, but not everything I had hoped and dreamed for. No matter, receiving a number is certainly a result I could expect, as opposed to the string joining from addition.