Converting a string to a date in JavaScript

JavascriptFront End TechnologyObject Oriented Programming

The best format to represent a date is yyyy-mm-dd as this causes no confusion and is pretty straight forward. In order to parse dates from this string format to Date objects in JS, all you need to do is pass this string to the Date constructor. For example,

Example

let a = '2019-08-10';
console.log(new Date(a))

Output

This will give the output −

Sat Aug 10 2019 05:30:00 GMT+0530 (India Standard Time)

Note that the new date is created at 0000 hrs UTC.

If you need to parse any other date formats, you can check out https://developer.mozilla.org as it provides details about using different formats to parse dates.

raja
Published on 02-Dec-2019 11:22:15
Advertisements