Wednesday, 13 May 2015

JavaScript Date Formats

JavaScript Date Formats

Valid JavaScript Date Strings

There are generally three types of valid JavaScript date formats:
  • ISO Dates
  • Long Dates
  • Short Dates

JavaScript ISO Dates

The ISO 8601 syntax (YYYY-MM-DD) is the newest (and preferred) JavaScript date format:

Example

var d = new Date("2015-03-25"); 
 
Or as: "2014-12" (YYYY-MM):

Example

var d = new Date("2015-03");
  

Or as "2014" (YYYY):

Example

var d = new Date("2015"); 
 
Or as "2014-12-24T12:00:00":

Example

var d = new Date("2015-03-25T12:00:00"); 
 

JavaScript Long Dates.

Long dates are most often written with a "MMM DD YYYY" syntax like this:

Example

var d = new Date("Mar 25 2015"); 
 
But, year, month, and day can be in any order:

Example

var d = new Date("25 Mar 2015"); 
 

Example

var d = new Date("2015 Mar 25"); 
 
And, month can be written in full (January), or abbreviated (Jan):

Example

var d = new Date("January 25 2015"); 
 

Example

var d = new Date("Jan 25 2015"); 
 
Commas are ignored. Names are case insensitive:

Example

var d = new Date("2015, JANUARY, 25"); 
 

JavaScript Short Dates.

Short dates are most often written with an "MM/DD/YYYY" syntax like this:

Example

var d = new Date("03/25/2015");
 
Either "/" or "-" can be used as a separator:

Example

var d = new Date("03-25-2015"); 
 
JavaScript will also accept "YYYY/MM/DD":

Example

var d = new Date("2015/03/25"); 
 

Full Format

JavaScript will accept date strings in "full JavaScript format":

Example

var d = new Date("Wed Mar 25 2015 09:56:24 GMT+0100 (W. Europe Standard Time)"); 
 
JavaScript will ignore errors both in the day name and in the time parentheses:

Example

var d = new Date("Fri Mar 25 2015 09:56:24 GMT+0100 (Tokyo Time)");
 
 
 
 
 
 
 
 
 
 
 
 

 

No comments:

Post a Comment