Date-Formatting Methods

Description

There are several Date type methods to format the date as a string.

  • 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.

Example


var date1 = new Date(2007, 0, 1);          //"January 1, 2007"
console.log(date1.toDateString());
console.log(date1.toTimeString());/*from   w  ww  .j  a v  a2s.c  o m*/
console.log(date1.toLocaleDateString());
console.log(date1.toLocaleTimeString());
console.log(date1.toUTCString());

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions