Skip to content

Instantly share code, notes, and snippets.

@desaijay315
Created September 9, 2020 06:11
Show Gist options
  • Save desaijay315/94cd69a8729b67a4c2d41ef9d73778b3 to your computer and use it in GitHub Desktop.
Save desaijay315/94cd69a8729b67a4c2d41ef9d73778b3 to your computer and use it in GitHub Desktop.
Javascript unix timestamp to date time conversion
let unix_timestamp = 1599629512236
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp);
// Hours part from the timestamp
var hours = date.getHours();
var day = date.getDate();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = day + ':' + hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment