How To Get A UTC Timestamp Using JavaScript

Get a UTC timestamp using JavaScript

Are you having trouble and don’t know how to get a UTC timestamp using JavaScript? If yes, let’s follow this tutorial. We will show you several methods to do this through the getTime(), Date.UTC() functions. It’s helpful for you.

Get a UTC timestamp using JavaScript

Using getTime() method

Use the getTime() method to get a UTC timestamp. The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation.

Syntax:

Date.getTime()

Parameters: None

Returns: A number of milliseconds.

Example:

// Use the getTime() method to get a UTC timestamp
const utcTimestamp = new Date().getTime();
console.log(utcTimestamp);

Output:

1664634174953

If you need any of the date and time components in UTC, use the available getUTC* methods. Following the code below.

Example:

const dateTime = new Date();

// Returns UTC Hour of the date
console.log(dateTime.getUTCHours()); // 14

// Returns UTC Minutes of the date
console.log(dateTime.getUTCMinutes()); // 41

// Returns UTC Seconds of the date
console.log(dateTime.getUTCSeconds()); // 28

// Returns UTC year of the date
console.log(dateTime.getUTCFullYear()); // 2022

// Returns UTC month (0-11), 0 is January, 11 is December
console.log(dateTime.getUTCMonth()); // 9

// Returns UTC day of the month (1-31)
console.log(dateTime.getUTCDate()); // 1

Output:

14
41
39
2022
9
1

Using Date.UTC() method

Syntax:

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

Parameters:

  • year: A number concludes with a four-digit value representing the year.
  • month: An integer from 0 to 11 representing the month. Other values:
    • -1: Last month of the previous year.
    • 12: The first month of the following year.
    • 13: The second month of the following year.
  • day: An integer from 1 to 31 representing the day. Other values:
    • 0: The last hour of the earlier month.
    • -1: The hour prior to the last hour of the earlier month.
    • 32: The first day of the next month, assuming the month has 31 days or the subsequent day on the off chance that the month has 30 days.
  • hours: An integer between 0 and 23 representing the hours. Other values:
    • -1: The last hour of the earlier day.
    • 24: The main hour of the following day.
  • minutes: An integer from 0 to 59 representing the minutes. Other values:
    • -1: The last moment of the earlier hour.
    • 60: The principal moment of the following hour.
  • seconds: An integer from 0 to 59 representing the seconds. Other values:
    • -1: The last second of the earlier moment.
    • 60: The first second of the following moment.
  • milliseconds: An integer from 0 to 999, representing the milliseconds. Other values:
    • -1: The last millisecond of the earlier second.
    • 1000: The first millisecond of the following second.

Return: A number addressing the quantity of milliseconds in the given Date object since January 1, 1970, 00:00:00.

Example:

// Returns the number of milliseconds
const date1 = Date.UTC(2022, 10, 01);
console.log(date1);

// Return year, month, and day respectively
const date2 = new Date(Date.UTC(2010, 10, 01));
console.log(date2);

Output:

1667260800000
Mon Nov 01 2010 07:00:00 GMT+0700 (Indochina Time)

Summary

I’ve explained methods to get a UTC timestamp using JavaScript in this tutorial. So choose the way that best suits your case. If you have any questions, leave a comment below. Have a great day!

Maybe you are interested:

Leave a Reply

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