Monday, 31 March 2014

Passing date value as a string to JavaScript Date() object

Date() Constructor has following variations

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

Date constructor parameters

Note: Where Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g.  new Date(2013,13,1) is equivalent to new Date(2014,1,1), both create a date for 2014-02-01 (note that the month is 0-based).
value

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.

dateString

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

The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.

Date.parse() understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 +0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed.

<script>
alert(new Date("March 18, 2014"))
alert(new Date("March 18, 2014 00:00:00 +0430"))
alert(new Date("18 March 2014"))
alert(new Date("2014-03-18"))
</script>

year

Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999.

month

Integer value representing the month, beginning with 0 for January to 11 for December.

day

Integer value representing the day of the month.

More info

Date Object

Date.parse

JavaScript Dates

No comments:

Post a Comment