To convert a JavaScript date object to a UTC string, you can use the toUTCString() method of the Date object.

The toUTCString() method converts a date to a string, using the universal time zone.

Here is example:

const date = new Date();

// Date in local time zone
// Sun Jun 20 2021 16:36:21 GMT+0500 (Pakistan Standard Time)

// Date in UTC time zone
// Sun, 20 Jun 2021 11:36:56 GMT

If you need to have the returned UTC string in ISO-8601 format, use the toISOString() method instead:

console.log(new Date().toISOString());
// 2021-06-20T11:40:39.937Z

The toISOString() method returns a string in ISO-8601 format (YYYY-MM-DDTHH:mm:ss.sssZ). The time zone is always UTC, as denoted by the suffix Z.

Read Next: How to get current time zone in JavaScript

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.