Categories
JavaScript Answers

How to create a UTC JavaScript date?

Spread the love

Sometimes, we want to create a UTC JavaScript date.

In this article, we’ll look at how to create a UTC JavaScript date.

How to create a UTC JavaScript date?

To create a UTC JavaScript date, we can use the Date.UTC method.

For instance, we write:

const d = new Date(Date.UTC(2022, 10, 5));
console.log(d.toUTCString());

to call Date.UTC to create a timestamp set to Nov 5, 2022 in UTC.

Then we convert that to a JavaScript date object with the Date constructor.

Therefore, d.toUTCString returns 'Sat, 05 Nov 2022 00:00:00 GMT'.

Conclusion

To create a UTC JavaScript date, we can use the Date.UTC method.

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 *