Sling Academy
Home/JavaScript/JavaScript: 2 Ways to Convert a String into a Date Object

JavaScript: 2 Ways to Convert a String into a Date Object

Last updated: February 28, 2023

This article shows you a couple of different approaches to converting a given string into a date object in JavaScript.

What strings can be converted to date objects?

First of all, it’s important to note that not all string formats can be converted to date objects in JavaScript. If a string is not in a recognized format, attempting to convert it to a date object may result in an invalid date or an error.

The table below lists valid string formats that are convertible to date objects (proper date strings):

FormatExample
ISO 8601“2023-02-28T13:30:00.000Z”
Short date“02/28/2023”
Long date"February 28, 2023"
RFC 2822“Mon, 28 Feb 2023 13:30:00 GMT”
Unix timestamp in seconds“1646079000”

These string formats can be used with various methods for converting a string to a date in JavaScript, including the Date() constructor and the Date.parse() method. Let’s examine a few practical examples for more clarity.

Using the Date() constructor

Example:

const dateString = '2023-02-28T13:30:00.000Z';
const date = new Date(dateString);

console.log(
  'Year: ' + date.getFullYear(),
  'Month: ' + date.getMonth(),
  'Day: ' + date.getDate()
);

Output:

Year: 2023 Month: 1 Day: 28

If the date string is in the wrong format, you will get an Invalid Date object:

const dateString = '2023-02-282';
const date = new Date(dateString);

console.log(date);

Output:

Invalid Date

Using the Date.parse() method

The Date.parse() method is another way to convert a string to a date in JavaScript. It takes a string as its argument and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. You can use this number to create a new date object.

Example:

const dateString = '2023-02-28T13:30:00.000Z';
const milliseconds = Date.parse(dateString);
const date = new Date(milliseconds);

console.log(
  'Year: ' + date.getFullYear(),
  'Month: ' + date.getMonth(),
  'Day: ' + date.getDate()
);

Output:

Year: 2023 Month: 1 Day: 28

That’s it. Happy coding!

Next Article: JavaScript: Convert timestamp to date time and vice versa

Previous Article: JavaScript: How to Convert Date Time to Time Ago

Series: Date and Time in JavaScript: Basic to Advanced Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration