w3resource

JavaScript: Convert a Unix timestamp to time

JavaScript Datetime: Exercise-17 with Solution

Unix to Time

Write a JavaScript function to convert a Unix timestamp to time.

Test Data:
console.log(Unix_timestamp(1412743274));
10:11:14

Sample Solution:

JavaScript Code:

// Define a JavaScript function called Unix_timestamp with parameter t
function Unix_timestamp(t)
{
// Create a new Date object representing the UNIX timestamp multiplied by 1000 (to convert seconds to milliseconds) and store it in the variable dt
var dt = new Date(t*1000);
// Extract the hours component from the Date object and store it in the variable hr
var hr = dt.getHours();
// Extract the minutes component from the Date object and store it in the variable m
var m = "0" + dt.getMinutes();
// Extract the seconds component from the Date object and store it in the variable s
var s = "0" + dt.getSeconds();
// Construct and return a string representation of the time in the format 'hours:minutes:seconds'
return hr+ ':' + m.substr(-2) + ':' + s.substr(-2);  
}

// Output the formatted time for the UNIX timestamp 1412743274
console.log(Unix_timestamp(1412743274));

Output:

10:11:14

Explanation:

In the exercise above,

  • The code defines a JavaScript function named "Unix_timestamp()" with one parameter 't', which represents a UNIX timestamp (number of seconds since January 1, 1970).
  • Inside the function:
    • It creates a new Date object named 'dt' by converting the UNIX timestamp to milliseconds (by multiplying it by 1000).
    • It extracts the hours component from the 'dt' Date object and stores it in the variable 'hr'.
    • It extracts the minutes component from the 'dt' Date object, pads it with a leading zero if necessary (using the "substr()" method), and stores it in the variable 'm'.
    • It extracts the seconds component from the 'dt' Date object, pads it with a leading zero if necessary (using the "substr()" method), and stores it in the variable 's'.
    • It constructs a string representation of the time in the format 'hours:minutes:seconds' and returns it.
  • The code then demonstrates the usage of the "Unix_timestamp()" function by calling it with the UNIX timestamp 1412743274.

Flowchart:

Flowchart: JavaScript- Convert a Unix timestamp to time

Live Demo:

See the Pen JavaScript - Convert a Unix timestamp to time-date-ex- 17 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that converts a Unix timestamp (in seconds) to a human-readable time string.
  • Write a JavaScript function that creates a Date object from a Unix timestamp and outputs the time in HH:MM:SS format.
  • Write a JavaScript function that handles both Unix timestamps in seconds and milliseconds for conversion.
  • Write a JavaScript function that validates the timestamp input and returns an error if it is not a number.

Go to:


PREV : Days Passed in Year.
NEXT : Calculate Age.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.