Created
January 12, 2016 20:41
-
-
Save jackm/309868db185c21f32456 to your computer and use it in GitHub Desktop.
Javascript function for a human readable timestamp in ISO8601 format
This file contains hidden or 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
// Human readable timestamp in ISO8601 format | |
function timeStamp() { | |
var now = new Date(); | |
var date = [now.getFullYear(), now.getMonth() + 1, now.getDate()]; | |
var time = [now.getHours(), now.getMinutes(), now.getSeconds()]; | |
// Prefix a zero to month and day | |
date[1] = ("00" + date[1]).slice(-2); | |
date[2] = ("00" + date[2]).slice(-2); | |
// If seconds and minutes are less than 10, add a zero | |
for (var i = 1; i < time.length; i++) { | |
if (time[i] < 10) { | |
time[i] = "0" + time[i]; | |
} | |
} | |
return date.join("-") + " " + time.join(":"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment