How To Get A GMT Timestamp Using JavaScript

Get a GMT timestamp using JavaScript

Today, we will give you some methods to get a GMT timestamp using JavaScript in this article. We will discuss Date.UTC(), toUTCString(), some other methods and how to use it to get a GMT timestamp. It will helpful for you. Let’s read it now.

Get a GMT timestamp using JavaScript

Use Date.UTC() method

Here, we will use the Date.UTC() method to get a GMT timestamp. This method has parameters similar to the Date method. Date.UTC() is a static method for Date class, so it’s called Date.UTC() instead of as a technique for a Date occurrence. Follow the code below to better understand.

Syntax:

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

Parameters

  • year: An integer performing the year.
  • month: An integer performing the month.
  • day: An integer performing the day. 
  • hours: An integer performing the hours.
  • minutes: An integer performing the minutes. 
  • seconds: An integer performing the seconds. 
  • milliseconds: An integer performing the milliseconds.

Return: A number performing of milliseconds.

Example:

const dateTime = new Date(Date.UTC(2022, 9, 2, 15, 30, 0, 0));
console.log(dateTime);

Output:

Sun Oct 02 2022 22:30:00 GMT+0700 (Indochina Time)

Use toUTCString() method

Use the toUTCString() method to get a GMT timestamp representation of the date.

The toUTCString() method is utilized to change the given date object’s content into a string as indicated by the widespread time region UTC. The date object is made utilizing the Date() constructor.

Syntax:

toUTCString()

Parameters: None

Return: A string which is current time with GMT.

Example:

// Get a strong performance on the given Date
// UTC (= GMT) time zone.
const dateTime = new Date();
const result = dateTime.toUTCString();
console.log(result);

Output:

Sun, 02 Oct 2022 12:26:58 GMT

Use the available getUTC* methods

Use getUTC* methods to get the date’s hours, minutes, seconds, year, month, and day. Following the code below.

Example:

const dateTime = new Date();
const result = dateTime.toUTCString();
console.log(result); // "Sun, 02 Oct 2022 12:35:10 GMT"

// Hour of the date
console.log(dateTime.getUTCHours()); // 12

// Minutes of the date
console.log(dateTime.getUTCMinutes()); // 35

// Seconds of the date
console.log(dateTime.getUTCSeconds()); // 31

// Year of the date
console.log(dateTime.getUTCFullYear()); // 2022

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

// Day of the month (1-31)
console.log(dateTime.getUTCDate()); // 2

Output:

Sun, 02 Oct 2022 12:35:37 GMT
12
35
37
2022
9
2

Summary

I have explained methods to get a GMT timestamp using JavaScript in this tutorial. So choose the way that best suits your case. Hopefully, they will help you. Leave a comment below if you have any problems. Thank you for your reading!

Maybe you are interested:

Leave a Reply

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