Convert to a string
suggest changeEach snippet calls new Date(), which is the current instant. The quoted strings are sample formats from one moment in April 2016, not the output of running those calls now. toString, toTimeString, and toDateString also depend on the local timezone.
Convert to String
const date1 = new Date();
date1.toString();
Returns: “Fri Apr 15 2016 07:48:48 GMT-0400 (Eastern Daylight Time)”
Convert to Time String
const date1 = new Date();
date1.toTimeString();
Returns: “07:48:48 GMT-0400 (Eastern Daylight Time)”
Convert to Date String
const date1 = new Date();
date1.toDateString();
Returns: “Thu Apr 14 2016”
Convert to UTC String
const date1 = new Date();
date1.toUTCString();
Returns: “Fri, 15 Apr 2016 11:48:48 GMT”
Convert to ISO String
const date1 = new Date();
date1.toISOString();
Returns: “2016-04-14T23:49:08.596Z”
Convert to GMT String
const date1 = new Date();
date1.toGMTString();
Returns: “Thu, 14 Apr 2016 23:49:08 GMT”
toGMTString is a deprecated alias of toUTCString. Use toUTCString().
Convert to Locale Date String
const date1 = new Date();
date1.toLocaleDateString();
Returns: “4/14/2016”
This function returns a locale sensitive date string based upon the user’s location by default.
date1.toLocaleDateString(locales, options) can request specific locales, but the result is implementation-specific. For example,
date1.toLocaleDateString(["zh", "en-US"]);
attempts to format the date in the Chinese locale, using United States English as a fallback. The options argument requests specific fields. For example:
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
date1.toLocaleDateString([], options);
would result in
“Thursday, April 14, 2016”.
See the MDN for more details.