| by Arround The Web | No comments

Convert an ISO String to a Date Object in JavaScript

To interact with date and time, including days, months, years, hours, minutes, seconds, and milliseconds, JavaScript offers the Date object. It is used to keep track of dates and execute different tasks on them. More specifically, ISO is an abbreviation for the International Organization for Standardization. According to the ISO standard, the year is placed first in the date string, followed by the smallest term. To convert a date object from an ISO string using JavaScript, utilize the constructor of the Date object.

This article will teach the methods for converting a Date Object from an ISO string using JavaScript.

How to Convert/Create a Date Object From an ISO String in JavaScript?

For converting an ISO string to a Date object, use the given JavaScript Date Object methods:

    • Date() constructor
    • parse() method

Method 1: Convert Date Object From an ISO String Using Date() Constructor

For converting an ISO string to a Date Object, the constructor of the Date() object is used. For conversion, pass the ISO string to the “newDate()” method.

Syntax

Use the following syntax for the Date() constructor:

new Date(ISOdateString);

 
It takes the date in an ISO format as a parameter.

Return Value

It gives a new Date object.

Example

Create a string that stores the date in an ISO format:

const isoString = '2022-10-10';

 
Call the date constructor by passing the ISO string and store the date object in the variable “dateObj”:

const dateObj = new Date(isoString);

 
Print the resultant date object on the console:

console.log(dateObj);

 
The output indicates that the ISO string is successfully converted to the Date object using the Date() constructor:

Method 2: Convert Date Object From an ISO String Using Date.parse() Method

For the conversion of the Date object from an ISO string, the “Date.parse()” method is used. The parse() method analyzes a date string and outputs the milliseconds since midnight on January 1, 1970.

Syntax

Follow the given-provided syntax to use the parse() method:

Date.parse(ISOdateString);

 
In the above syntax, “ISOdateString” is the date in an ISO string format.

Return Value

    • It gives back a value that is the sum of the milliseconds since January 1st, 1970, at 00:00:00 UTC, and the date obtained by analyzing the available string used to denote a date.
    • It gives NaN while receiving an argument with an invalid date format.

Example

Pass the ISO string in a parse() method to get the Date object in milliseconds:

const dateObj = Date.parse(isoString);

 
Print the result on the console:

console.log(dateObj);

 
Output


We have compiled the essential information related to converting an ISO string to a date object in JavaScript.

Conclusion

In the conversion of the Date object from an ISO string, the constructor of the Date Object as “newDate()” or the “Date.parse()” method is used. The parse() method gives the sum of the milliseconds from January 1st, 1970, at 00:00:00 UTC, and the date as a string, while Date() gives the new Date object. This article teaches the methods for converting an ISO string to a Date Object using JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply