Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jackm
Created January 12, 2016 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackm/309868db185c21f32456 to your computer and use it in GitHub Desktop.
Save jackm/309868db185c21f32456 to your computer and use it in GitHub Desktop.
Javascript function for a human readable timestamp in ISO8601 format
// 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