Categories
JavaScript Answers

How to fix JSON.stringify changes time of date because of UTC with JavaScript?

Spread the love

Sometimes, we want to fix JSON.stringify changes time of date because of UTC with JavaScript.

In this article, we’ll look at how to fix JSON.stringify changes time of date because of UTC with JavaScript.

How to fix JSON.stringify changes time of date because of UTC with JavaScript?

To fix JSON.stringify changes time of date because of UTC with JavaScript, we can use JSON.parse to convert the date back to its original format.

For instance, we write

let currentDate = new Date();
currentDate = JSON.stringify(currentDate);
currentDate = new Date(JSON.parse(currentDate));

to call create the currentDate date object.

Then we call JSON.stringify to convert the currentDate object into a JSON string.

Then we call JSON.parse to parse the date string back to its original format.

And then we use the Date constructor to convert it back to a date object.

Conclusion

To fix JSON.stringify changes time of date because of UTC with JavaScript, we can use JSON.parse to convert the date back to its original format.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *