Fun with the javascript date object.
Posted on July 10, 2008 in TechnologySo 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!

RSS (2.0)
Previous Comments
So, i stop visiting your site because i thought i was abandoned. For some reason i decided to see if anything new was up. Low and behold new content, some garb about javascript.
How about a nice post about what you are up to these days. A pic would be nice too (haven't seen you in a while).
Whats up bro? Whats new?