How to convert Unix timestamp to time in JavaScript ?
Last Updated :
31 May, 2024
In this article, we will see how to convert UNIX timestamps to time.
These are the following approaches:
- 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();
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.
JavaScript
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();
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:
JavaScript
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();
Similar Reads
How to Get the Timestamp in JavaScript? A timestamp is a numeric representation of the current time. It is a unique identifier that marks the exact moment when an event occurred or when a certain action was performed. 1. Using Date.now() MethodThis approach uses Date.now() method. This method returns the number of milliseconds since Janua
2 min read
How to Convert DateTime to UNIX Timestamp in Python ? Generating a UNIX timestamp from a DateTime object in Python involves converting a date and time representation into the number of seconds elapsed since January 1, 1970 (known as the Unix epoch). For example, given a DateTime representing May 29, 2025, 15:30, the UNIX timestamp is the floating-point
2 min read
How to Convert Date to Another Timezone in JavaScript? Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries.1. Using Intl.DateTimeFormat() and format(
2 min read
How to convert UTC date time into local date time using JavaScript ? Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. html
1 min read
How to Get Current Time in JavaScript ? This article will show you how to get the current time in JavaScript. Whether you are creating a digital clock, scheduling events, or simply logging timestamps, knowing how to retrieve the current time is essential. Here, we will cover different methods to get the current time in JavaScript.Table of
2 min read
How to convert a JavaScript date to UTC? Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone. By defa
1 min read