Created
June 4, 2013 21:31
-
-
Save zmcghee/5709784 to your computer and use it in GitHub Desktop.
JavaScript date format from UNIX timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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