JavaScript:
Parsing a date from a string
How to:
JavaScript natively offers the Date.parse()
method and the Date
constructor to parse date strings. However, these approaches have limitations and inconsistencies across different browsers, especially with non-standard date formats. To address these issues, third-party libraries like Moment.js
and date-fns
are popular for their robustness and ease of use.
Using native JavaScript:
const dateString = "2023-04-30T14:55:00";
const dateObj = new Date(dateString);
console.log(dateObj); // Output: Sun Apr 30 2023 14:55:00 GMT+0000 (Coordinated Universal Time)
Using Moment.js:
First, install Moment.js via npm or include it in your project. Then:
const moment = require('moment');
const dateString = "2023-04-30T14:55:00";
const dateObj = moment(dateString);
console.log(dateObj.toString()); // Output: Sun Apr 30 2023 14:55:00 GMT+0000
Using date-fns:
After adding date-fns
to your project, parse a date string like so:
const { parseISO } = require('date-fns');
const dateString = "2023-04-30T14:55:00";
const dateObj = parseISO(dateString);
console.log(dateObj); // Output: 2023-04-30T14:55:00.000Z
Both Moment.js
and date-fns
provide more comprehensive parsing capabilities, including handling a variety of formats and locales, which makes them preferable for complex applications.