w3resource

JavaScript: Convert a Unix timestamp to time

JavaScript Datetime: Exercise-17 with Solution

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.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to count the number of days passed since beginning of the year.
Next: Write a JavaScript program to calculate age.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript-exercises/javascript-date-exercise-17.php