Javascript - Date to String

Introduction

The Date type's toLocaleString() method returns the date and time in a format appropriate for the locale.

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

toLocaleString() and toString() are useful only for debugging purposes.

Demo

var date1 = new Date(2007, 0, 1);          //"January 1, 2007"
console.log(date1.toString());
console.log(date1.toLocaleString());

Result

Date toXXXString()

There are several Date type methods which can format the date as a string. They are as follows:

Method Usage
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.
toGMTString() equivalent to toUTCString() for backwards compatibility.

Demo

var date1 = new Date();

console.log(date1.toDateString());//from w  w w .j a  v a  2 s. co  m
console.log(date1.toTimeString());
console.log(date1.toLocaleDateString());
console.log(date1.toLocaleTimeString());
console.log(date1.toUTCString());
console.log(date1.toGMTString());

Result