Skip to content
the poor coder

convert iso string to datetime javascript

Poor Man
Published
Reading time
1 min read
On this page

Convert ISO String to Datetime in JavaScript#

If you have an ISO string and want to convert it to a datetime object in JavaScript, you can use the built-in Date object.

Method 1: Using Date constructor#

You can use the Date constructor to create a new Date object with the ISO string as its argument. Here's an example:

Text

const isoString = '2022-05-12T03:30:00Z';
const datetime = new Date(isoString);
console.log(datetime); // Output: Thu May 12 2022 08:00:00 GMT+0530 (India Standard Time)

The Date object will automatically parse the ISO string and convert it to the local timezone.

Method 2: Using moment.js library#

If you prefer using a library, you can use moment.js to parse the ISO string and convert it to a datetime object. Here's an example:

Text

const isoString = '2022-05-12T03:30:00Z';
const datetime = moment(isoString).toDate();
console.log(datetime); // Output: Thu May 12 2022 08:00:00 GMT+0530 (India Standard Time)

The moment function returns a moment object, which can be converted to a Date object using the toDate() method.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC