Understanding UTC and GMT
Before we get into the code, it's important to understand what UTC and GMT are. UTC, or Coordinated Universal Time, is the time standard used in aviation, computing, navigation, weather forecasting, and many other fields. It's essentially the time at the prime meridian (0° longitude), but with a few technical differences that are beyond the scope of this article.
GMT, or Greenwich Mean Time, is the mean solar time at the Royal Observatory in Greenwich, London. It was once the international time standard, but has been largely replaced by UTC.
Note: For most practical purposes, especially in computing, UTC and GMT are considered equivalent.
Generating a UTC/GMT Timestamp
Date object is used to work with dates and times. The
Date.now() method returns the number of milliseconds since the Unix Epoch (January 1, 1970 00:00:00 UTC). However, value is not in UTC or GMT by default. To convert it, we can use the
Here's an example:
let date = new Date(); let timestamp = date.getTime(); let utcDate = new Date(timestamp); console.log(utcDate.toUTCString());
When you run this, it will output a string representing the current date and time in UTC, like this:
"Wed, 23 Aug 2023 22:43:21 GMT"
Date object and its methods.