JavaScript Date.UTC(): How to Convert a Date to UTC

JavaScript Date.UTC() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The Date.UTC() method is “used to convert the specified date-time to UTC in milliseconds.”

Syntax

Date.UTC(year, month, day, hours, minutes, seconds, millisec)

Parameters

  1. year:

    • Type: Integer
    • Description: Represents the year of the date. For years between 0 and 99, the method interprets them as 1900-1999. So, Date.UTC(89, 0) would be interpreted as January 1, 1989.
  2. month:
    • Type: Integer
    • Description: Represents the month of the year, starting from 0 for January to 11 for December.
  3. day (optional):
    • Type: Integer
    • Default: 1
    • Description: Represents the day of the month. If omitted, it defaults to 1.
  4. hour (optional):
    • Type: Integer
    • Default: 0
    • Description: Represents the hour of the day from 0 to 23. If omitted, it defaults to 0.
  5. minute (optional):
    • Type: Integer
    • Default: 0
    • Description: Represents the minute of the hour from 0 to 59. If omitted, it defaults to 0.
  6. second (optional):
    • Type: Integer
    • Default: 0
    • Description: Represents the second of the minute from 0 to 59. If omitted, it defaults to 0.
  7. millisecond (optional):

    • Type: Integer
    • Default: 0
    • Description: Represents the milliseconds of the second, from 0 to 999. If omitted, it defaults to 0.

Return value

The Date.UTC() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the given date and time. This value can then be used to create a new Date object, representing that exact time in UTC.

Example 1: How to Convert a Date to UTC

let year = 2023;
let month = 7; // August (months are 0-indexed)
let day = 26;
let hour = 12;
let minute = 30;
let second = 45;

let utcMilliseconds = Date.UTC(year, month, day, hour, minute, second);
let dateFromUTC = new Date(utcMilliseconds);

console.log(dateFromUTC.toISOString());

Output

2023-08-26T12:30:45.000Z

Example 2: Printing UTC in milliseconds

let year = 2023;
let month = 7; // August (months are 0-indexed)
let day = 26;
let hour = 12;
let minute = 30;
let second = 45;

let utcMilliseconds = Date.UTC(year, month, day, hour, minute, second);
console.log(utcMilliseconds)

Output

1693053045000

Browser Compatibility

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Convert Date to UTC Using toUTCString() Method

JavaScript toUTCString() method of the Date object is “used to convert a date to a string representation using the UTC time zone.”

// Assuming this is in local time
let localDate = new Date('2023-08-26T15:05:52');

console.log("Local Date:", localDate.toString());

let utcString = localDate.toUTCString();

console.log("UTC Date:", utcString);

Output

Convert Date to UTC Using toUTCString() Method

That’s it!

Related posts

JavaScript Date toLocaleDateString()

JavaScript Date toTimeString()

JavaScript Date toISOString()

JavaScript Date setDate()

JavaScript Date getDay()

Leave a Comment