Friday, July 31, 2009

Ambiguity in Javascript Date comparison

The Javascript Date.UTC function helps so convert a date to millisecond.

We use it in our application to do date comparison in relationship review

Syntax :

Date.UTC(yr, mnth, date)

We’d an issue in our application a particular date comparison was not going correctly

The Javascript says 01/31/2010 is greater than 02/02/2010!!

I searched the internet for a solution but in vain later I stumbled upon the fact that for Javascript the month starts with 0, i.e, Jan - 0, Feb – 1 etc

So in our code we said Date.UTC(2010, 01, 31) and it would’ve thought Feb 31st which was not possible. But it gave some millisecond result which became wrong.

I later changed the code accordingly and it works fine!

No comments: