How to convert Unix timestamp to time in JavaScript ?
Last Updated :
31 May, 2024
Improve
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
- 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.
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();
15
1
function convertTimestamptoTime() {
2
3
let unixTimestamp = 10637282;
4
5
// Convert to milliseconds and
6
// then create a new Date object
7
let dateObj = new Date(unixTimestamp * 1000);
8
let utcString = dateObj.toUTCString();
9
10
let time = utcString.slice(-11, -4);
11
12
console.log(time);
13
}
14
15
convertTimestamptoTime();
Output
2:48:02
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.
Syntax:
dateObj = new Date(unixTimestamp * 1000);
// Get hours from the timestamp
hours = dateObj.getUTCHours();
// Get minutes part from the timestamp
minutes = dateObj.getUTCMinutes();
// Get seconds part from the timestamp
seconds = dateObj.getUTCSeconds();
formattedTime = hours.toString()
.padStart(2, '0') + ':'
+ minutes.toString()
.padStart(2, '0') + ':'
+ seconds.toString()
.padStart(2, '0');
Example: This example shows the conversion of time.
function convertTimestamptoTime() {
let unixTimestamp = 10637282;
// Convert to milliseconds and
// then create a new Date object
let dateObj = new Date(unixTimestamp * 1000);
// Get hours from the timestamp
let hours = dateObj.getUTCHours();
// Get minutes part from the timestamp
let minutes = dateObj.getUTCMinutes();
// Get seconds part from the timestamp
let seconds = dateObj.getUTCSeconds();
let formattedTime = hours.toString().padStart(2, '0')
+ ':' + minutes.toString().padStart(2, '0')
+ ':' + seconds.toString().padStart(2, '0');
console.log(formattedTime);
}
convertTimestamptoTime();
25
1
function convertTimestamptoTime() {
2
3
let unixTimestamp = 10637282;
4
5
// Convert to milliseconds and
6
// then create a new Date object
7
let dateObj = new Date(unixTimestamp * 1000);
8
9
// Get hours from the timestamp
10
let hours = dateObj.getUTCHours();
11
12
// Get minutes part from the timestamp
13
let minutes = dateObj.getUTCMinutes();
14
15
// Get seconds part from the timestamp
16
let seconds = dateObj.getUTCSeconds();
17
18
let formattedTime = hours.toString().padStart(2, '0')
19
+ ':' + minutes.toString().padStart(2, '0')
20
+ ':' + seconds.toString().padStart(2, '0');
21
22
console.log(formattedTime);
23
}
24
25
convertTimestamptoTime();
Output
02:48:02
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.
- Convert the UNIX timestamp to milliseconds.
- Create a Date object.
- Use Intl.DateTimeFormat to format the time in the desired locale and options.
Example:
function convertTimestamptoTime() {
let unixTimestamp = 10637282;
// Convert to milliseconds and then create a new Date object
let dateObj = new Date(unixTimestamp * 1000);
// Create a formatter for the desired locale and options
let options = {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZone: 'UTC'
};
let formatter = new Intl.DateTimeFormat('en-US', options);
// Format the date object to extract the time
let formattedTime = formatter.format(dateObj);
console.log(formattedTime);
}
convertTimestamptoTime();
23
1
function convertTimestamptoTime() {
2
let unixTimestamp = 10637282;
3
4
// Convert to milliseconds and then create a new Date object
5
let dateObj = new Date(unixTimestamp * 1000);
6
7
// Create a formatter for the desired locale and options
8
let options = {
9
hour: '2-digit',
10
minute: '2-digit',
11
second: '2-digit',
12
hour12: false,
13
timeZone: 'UTC'
14
};
15
let formatter = new Intl.DateTimeFormat('en-US', options);
16
17
// Format the date object to extract the time
18
let formattedTime = formatter.format(dateObj);
19
20
console.log(formattedTime);
21
}
22
23
convertTimestamptoTime();
Output
02:48:02