Skip to content

Instantly share code, notes, and snippets.

@genediazjr
Last active December 31, 2015 11:59
Show Gist options
  • Save genediazjr/7983639 to your computer and use it in GitHub Desktop.
Save genediazjr/7983639 to your computer and use it in GitHub Desktop.
A simple yyyyMMddHHmmssSSS timestamp generator
function getTimestamp () {
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth() + 1) + '';
var day = date.getDate() + '';
var hr = date.getHours() + '';
var min = date.getMinutes() + '';
var sec = date.getSeconds() + '';
var ms = date.getMilliseconds() + '';
if (month.length === 1) {
month = '0' + month;
}
if (day.length === 1) {
day = '0' + day;
}
if (hr.length === 1) {
hr = '0' + hr;
}
if (min.length === 1) {
min = '0' + min;
}
if (sec.length === 1) {
sec = '0' + sec;
}
if (ms.length === 1) {
ms = '00' + ms;
} else if (ms.length === 2) {
ms = '0' + ms;
}
return year + month + day + hr + min + sec + ms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment