Javascript Variable Date

advertisements
var dt1=new Date("2013/05/25"); //yyyy/MM/dd
var dt2=new Date("2013/05/24");

if(dt1>dt2)
{
    alert("invalid Date!");
}

This code is working in all Browsers..

But if we select:

var dt1=new Date("25/05/2013"); //dd/MM/yyyy
var dt2=new Date("24/05/2013");

if(dt1>dt2)
{
    alert("invalid Date!");
}

This code is not working in all the browsers? Why? Is there any specifc date format that should be accepted by all the browsers? Is there any standard for DateTime as well? Thank you........


Yes there is a standard for Javascript Date Object.

One of the contructors is new Date(dateString); and for dateString there is an explanation:

String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).

And in IE is a little different IE WAY

So the most secure way to do what you want is either use the last constructor:

new Date(year, month, day [, hour, minute, second, millisecond]);

or your dateString must be yyyy/mm/dd