How to use the toUTCString() method In Javascript

  • As JavaScript works in milliseconds, it is necessary to convert the time into milliseconds by multiplying it by 1000 before converting it.
  • This value is then given to the Date() function to create a new Date object. The toUTCString() method is used to represent the Date object as a string in the UTC time format.
  • The time from this date string can be found by extracting from the 11th to last to the 4th to the last character of the string.
  • This is extracted using the slice() function. This string is the time representation of the UNIX timestamp. 

Syntax: 

dateObj = new Date(unixTimestamp * 1000);
utcString = dateObj.toUTCString();
time = utcString.slice(-11, -4);

Example: This example shows the conversion of time.

Javascript
function convertTimestamptoTime() {

    let unixTimestamp = 10637282;

    // Convert to milliseconds and
    // then create a new Date object
    let dateObj = new Date(unixTimestamp * 1000);
    let utcString = dateObj.toUTCString();

    let time = utcString.slice(-11, -4);

    console.log(time);
}

convertTimestamptoTime();

Output
2:48:02

How to convert Unix timestamp to time in JavaScript ?

In this article, we will see how to convert UNIX timestamps to time.

These are the following approaches:

Table of Content

  • Using the toUTCString() method
  • Getting individual hours, minutes, and seconds
  • Using Intl.DateTimeFormat Object

Similar Reads

Using the toUTCString() method

As JavaScript works in milliseconds, it is necessary to convert the time into milliseconds by multiplying it by 1000 before converting it. This value is then given to the Date() function to create a new Date object. The toUTCString() method is used to represent the Date object as a string in the UTC time format. The time from this date string can be found by extracting from the 11th to last to the 4th to the last character of the string. This is extracted using the slice() function. This string is the time representation of the UNIX timestamp....

Getting individual hours, minutes, and seconds

As JavaScript works in milliseconds, it is necessary to convert the time into milliseconds by multiplying it by 1000 before converting it. This value is then given to the Date() function to create a new Date object. Each part of the time is extracted from the Date object. The hour’s value in UTC is extracted from the date using the getUTCHours() method. The minute’s value in UTC is extracted from the date using the getUTCMinutes() method. The second’s value in UTC is extracted from the date using the getUTCSeconds() method. The final formatted date is created by converting each of these values to a string using the toString() method and then padding them with an extra ‘0’, if the value is a single-digit by using the padStart() method. The individual parts are then joined together with a colon(:) as the separator. This string is the time representation of the UNIX timestamp....

Using Intl.DateTimeFormat Object

The Intl.DateTimeFormat object allows for formatting dates and times according to locale-specific conventions. This method provides a flexible and powerful way to format the time extracted from a UNIX timestamp....