# How to convert a date to UTC string in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jun 20, 2021, updated Oct 8, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-convert-date-to-utc-string

> A quick guide to learning how to convert a JavaScript date object into a UTC string.

To convert a JavaScript date object to a UTC string:

1. Use the `toUTCString()` method of the `Date` object.
2. The `toUTCString()` method converts a date to a string using the universal time zone.

```javascript
const date = new Date()

// Date in the local time zone
console.log(date.toString())
// Sun Jun 20 2021 16:36:21 GMT+0500 (Pakistan Standard Time)

// Date in UTC time zone
console.log(date.toUTCString())
// Sun, 20 Jun 2021 11:36:56 GMT
```

Alternatively, you could use the `Date.UTC()` method to create a new `Date` object directly in the UTC time zone. By default, the `Date.UTC()` method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. 

In the following example, we use `new Date()` to convert the milliseconds into a JavaScript `Date` object.

```javascript
const date = new Date(Date.UTC(2021, 5, 20, 12, 44, 20))

const utc = date.toUTCString()

console.log(utc)
// Sun, 20 Jun 2021 12:44:20 GMT
```

If you need to have the returned UTC string in [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) format, use the `toISOString()` method instead:

```javascript
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](https://attacomsian.com/blog/javascript-current-timezone)
