Skip to content

Instantly share code, notes, and snippets.

@zmcghee
Created June 4, 2013 21:31
Show Gist options
  • Save zmcghee/5709784 to your computer and use it in GitHub Desktop.
Save zmcghee/5709784 to your computer and use it in GitHub Desktop.
JavaScript date format from UNIX timestamp
function date_str_from_unix(unix_timestamp) {
// var my_date_str = date_str_from_unix(1370383708);
// my_date_str returns "2013-06-04 17:08"
var leading_zero = function(i) {
return i.toString().length < 2 ? "0" + i : i
}
var date = new Date(unix_timestamp * 1000);
var date_str = date.getFullYear() + "-" + leading_zero(date.getMonth() + 1)
+ "-" + leading_zero(date.getDate()) + " "
+ leading_zero(date.getHours()) + ":"
+ leading_zero(date.getMinutes());
return date_str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment