Javascript - Date() - the toString() methods

The Javascript Date type doesn't support custom format strings like "yyyyMMddHHmmss".

But it does provide a number of methods that return the date as a string in a specific format. The toLocaleString() methods additionally accept a locale plus options that provides a considerable degree of customization. There is a link to that article at the bottom of the page.

The default
date.toString()
Thu Jan 04 2018 11:12:19 GMT-0500 (Eastern Standard Time)
Date only
date.toDateString()
Thu Jan 04 2018
date.toLocaleDateString( [locales, [options]] )
1/4/2018
Time only
date.toTimeString()
11:12:19 GMT-0500 (Eastern Standard Time)
date.toLocaleTimeString( [locales, [options]] )
11:12:19 AM
Date & Time
date.toUTCString()
Thu, 04 Jan 2018 16:12:19 GMT
date.toGMTString()
Thu, 04 Jan 2018 16:12:19 GMT
date.toISOString()
2018-01-04T16:12:19.790Z
date.toLocaleString( [locales, [options]] )
1/4/2018, 11:12:19 AM
JSON format
date.toJSON()
2018-01-04T16:12:19.790Z

The Date Object:

var now = new Date();
var str = now.toLocaleString(locales, options);
console.log(str);


Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath