Altered piece of code I often use:

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1; // getMonth() is zero-based
  var dd = this.getDate();

  return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('');
};

var date = new Date();
date.yyyymmdd();
Answer from o-o on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toDateString
Date.prototype.toDateString() - JavaScript - MDN - Mozilla
The toDateString() method of Date instances returns a string representing the date portion of this date interpreted in the local timezone. const event = new Date(1993, 6, 28, 14, 39, 7); console.log(event.toString()); // Expected output: "Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)" // Note: your ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN - Mozilla
There are many ways to format a date as a string. The JavaScript specification only specifies one format to be universally supported: the date time string format, a simplification of the ISO 8601 calendar date extended format.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toString
Date.prototype.toString() - JavaScript - MDN Web Docs - Mozilla
July 10, 2025 - const event = new Date("August 19, 1975 23:15:30"); console.log(event.toString()); // Expected output: "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)" // Note: your timezone may vary ... A string representing the given date (see description for the format).
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_date.asp
JavaScript Date toString() Method
cssText getPropertyPriority() ... Date Reference Next ❯ · Convert a date object to a string: const d = new Date(); let text = d.toString(); Try it Yourself » ·...
🌐
Scaler
scaler.com › home › topics › convert string to date in javascript
Convert String to Date in JavaScript - Scaler Topics
January 10, 2024 - The Date class also provides us with another way to convert a date from string to Date format. The Date class has a static method called parse. You can use this method in the same way that you would use the Date class's constructor.
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... The ISO format follows a strict standard in JavaScript. The other formats are not so well defined and might be browser specific. Independent of input format, JavaScript will (by default) output dates in full text string format:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-date-tostring-method
JavaScript Date toString() Method - GeeksforGeeks
July 11, 2025 - Example 2: Here nothing as a parameter is passed while creating a date object but still toString() method returns the current day name, month name, date, year, and time. ... // Here nothing has been assigned // while creating Date object let ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-string-into-date-using-javascript
Convert string into date using JavaScript - GeeksforGeeks
July 11, 2025 - Example: In this example, we will use date.parse() method to get time out of the string and convert it to get date output.
Top answer
1 of 16
1087

The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.

Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.

But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC.

To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z').

To display a date in UTC, use .toUTCString(),
to display a date in user's local time, use .toString().

More info on MDN | Date and this answer.

For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00'). Note that the number of the month must be 1 less.


Alternate method - use an appropriate library:

You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.

2 of 16
452

Unfortunately I found out that

var mydate = new Date('2014-04-03');
console.log(mydate.toDateString());

returns "Wed Apr 02 2014". I know it sounds crazy, but it happens for some users.

The bulletproof solution is the following:

var parts ='2014-04-03'.split('-');
// Please pay attention to the month (parts[1]); JavaScript counts months from 0:
// January - 0, February - 1, etc.
var mydate = new Date(parts[0], parts[1] - 1, parts[2]); 
console.log(mydate.toDateString());

🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-date
JavaScript Date: Create, Convert, Compare Dates in JavaScript
JS Object In Depth · this Keyword ... hours, minutes, seconds, and milliseconds. Use the Date() function to get the string representation of the current date and time in JavaScript....
🌐
Sentry
sentry.io › sentry answers › javascript › parsing a string to a `date` in javascript
Parsing a string to a `Date` in JavaScript | Sentry
February 15, 2023 - One way to call it is with a dateString argument. The dateString argument needs to be in the ISO 8601 format: ... The string that you want to parse into a Date should match this format or a portion of this format.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-convert-date-to-string
JavaScript Program to Convert Date to String - GeeksforGeeks
July 23, 2025 - The Date.toJSON() method converts a Date object to a string formatted according to the ISO 8601 standard, similar to Date.toISOString(). This method is useful when you need to serialize a Date object to JSON.
🌐
Index.dev
index.dev › blog › convert-string-to-date-javascript
6 Easy Ways To Convert String to Date in JavaScript
This blog post outlines six straightforward methods for converting a string to a date in JavaScript. Every approach possesses distinct advantages and applications: Date Constructor: Efficient and direct for ISO format. Date.parse(): Suitable for timestamps including temporal data. Moment.js: Optimal for intricate date operations.
🌐
W3Schools
w3schools.com › jsref › jsref_todatestring.asp
JavaScript Date toDateString()
cssText getPropertyPriority() getPropertyValue() item() length parentRule removeProperty() setProperty() JS Conversion ... The toDateString() method returns the date (not the time) of a date object as a string.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Date and time
The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ, where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter. HH:mm:ss.sss – is the time: hours, minutes, seconds and milliseconds.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › Date
Date() constructor - JavaScript - MDN - Mozilla
The Date() constructor creates Date objects. When called as a function, it returns a string representing the current time.
🌐
Day.js
day.js.org › docs › en › parse › string-format
String + Format · Day.js
You may specify a boolean for the last argument to use strict parsing. Strict parsing requires that the format and input match exactly, including delimiters. dayjs('1970-00-00', 'YYYY-MM-DD').isValid() // true dayjs('1970-00-00', 'YYYY-MM-DD', true).isValid() // false dayjs('1970-00-00', 'YYYY-MM-DD', 'es', true).isValid() // false · If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-string-to-date-date-parsing-in-js
JavaScript String to Date – Date Parsing in JS
June 29, 2022 - For instance, since the local system time is used to interpret the date, depending upon where in the world your computer is, you could get different results from the same non-specific date. So, when passing a string into new Date(), use a full date with hours:minutes.milliseconds.