So for a few projects recently I needed to make use of Javascript Date functionality. At first glance it seems like this archaic system of creating and managing dates but after some tinkering I am realizing yet again that tools created in the computer dawn of time are unbelievably robust.

One of the issues I keep running into is when taking a date which you are working with and trying to make a copy of a date object, when you modify the date of the clone the original is affected. That’s because it’s a reference and not a clone. Watch out for this as I was bit a few times.

var date1 = new Date(“07/05/08”) var myDate = date1; //this creates a reference to the original myDate.setDate(“15”); date1.getDate(); //equals 15

Some other cool things that you can do with the date object is that you can very easily add days to a date, and it will calculate if months change, leap years etc.

myDate.setDate(myDate.getDate() + 10) // adds 10 days.

You can also subtract two dates from each other.

christmas = new Date(“12/25/08”); today – new Date(); secondsToChristmas = christmas – today; //time to christmas in milliseconds daysToChristmas = secondsToChristmas /(1000*60*60*24); //yey math is fun!