分类: Javascript

Professional Javascript For Web Developers – Date Type






THE DATE TYPE

To create a date object, use the new operator along with the Date constructor

	var now = new Date();


When the Date constructor is used without any arguments, the created object is assigned the current date and time.
ECMAScript provides two methods: Date.parse() and Date.UTC().
The Date.parse() method accepts a string argument representing a date. It attempts to convert the string into a millisecond representation of a date.For instance, to create a date object for May 25, 2004, you can use the following code:

	var someDate = new Date(Date.parse(“May 25, 2004”));

The Date constructor will call Date.parse() behind the scenes if a string is passed in directly, meaning that the following code is identical to the previous example:

	
	var someDate = new Date(“May 25, 2004”);

The Date.UTC() method also returns the millisecond representation of a date but constructs that value using different information than Date.parse().
The arguments for Date.UTC() are the year, the zero-based month (January is 0, February is 1, and so on), the day of the month (1 through 31), and the hours (0 through 23), minutes, seconds, and milliseconds of the time.only the first two (year and month) are required. If the day of the month isn’t supplied, it’s assumed to be 1, while all other omitted arguments are assumed to be 0. Here are two examples of Date. UTC() in action:

	//January 1, 2000 at midnight GMT 
	var y2k = new Date(Date.UTC(2000, 0));

	//May 5, 2005 at 5:55:55 PM GMT 
	var allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55));

As with Date.parse(), Date.UTC() is mimicked by the Date constructor but with one major difference: the date and time created are in the local time zone, not in GMT.the Date constructor takes the same arguments as Date.UTC()

	//January 1, 2000 at midnight in local time 
	var y2k = new Date(2000, 0); 

	//May 5, 2005 at 5:55:55 PM local time 
	var allFives = new Date(2005, 4, 5, 17, 55, 55);

This code creates the same two dates as the previous example, but this time both dates are in the local time zone as determined by the system settings.
ECMAScript 5 adds Date.now(), which returns the millisecond representation of the date and time at which the method is executed.The Date type’s toLocaleString() method returns the date and time in a format appropriate for the locale in which the browser is being run
The toString() method typically returns the date and time with time-zone information, and the time is typically indicated in 24-hour notation (hours ranging from 0 to 23)
There are several Date type methods used specifically to format the date as a string. They are as follows:
➤ toDateString() — Displays the date’s day of the week, month, day of the month, and year in an implementation-specific format.
➤ toTimeString() — Displays the date’s hours, minutes, seconds, and time zone in an implementation-specific format.
➤ toLocaleDateString() — Displays the date’s day of the week, month, day of the month, and year in an implementation- and locale-specific format.
➤ toLocaleTimeString() — Displays the date’s hours, minutes, and seconds in an implementation-specific format.
➤ toUTCString() — Displays the complete UTC date in an implementation-specific format.




发表评论

Webmentions

  • Professional Javascript For Web Developers – 复习笔记 – Ian's Blog

    […] (syntax,variable and operations) – Variables, Scope, And Memory – Reference Types – Array Type – Date Type – Regular […]

  • Professional Javascript For Web Developers – 复习笔记(Reference Types) | Ian's Blog

    […] 4.The Date Type […]