Work with dates

In JavaScript you may use the Date object for dates. This is common knowledge. What is a little more difficult is to work with dates.

new Date();  
// returns a date object with the current time

There is also a way to get the current timestamp

new Date() * 1;  
// returns the current timestamp.

The Date object in JavaScript is not very good to work with.

Example

new Date(2014,03,24);  
// returns "Thu Apr 24 2014 00:00:00 GMT+0200 (CEST)"
new Date('2014-03-24');  
// returns "Mon Mar 24 2014 01:00:00 GMT+0100 (CET)"

So the Month is zero based.

var date = new Date(2014,03,24);  
// returns "Thu Apr 24 2014 00:00:00 GMT+0200 (CEST)"
date.getDay()  
// returns 4.
// days are weekdays and they are zero based and the weeks starts on sunday.
date = new Date(2014,03,20);  
date.getDay();  
// returns 0.

Days are zero based and the week starts on Sunday

With that in mind you would guess that getDate() is also zero based.

var date = new Date(2014,03,20);  
date.getDate()  
// returns 20

so the Date is NOT zero based neither is the getFullYear.

By the way getYear returns he current year minus 1900.

So what is really important is if you work with dates, be sure to test enough to get it right. And do not forget about the daylight saving times. In the spring you lose an hour which you win back in autumn, at least here in germany this may differ depending on the country.