Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active July 30, 2018 10:55
Show Gist options
  • Save andreasvirkus/39dde3a11ee64dc8b9dd1f2963d55cd5 to your computer and use it in GitHub Desktop.
Save andreasvirkus/39dde3a11ee64dc8b9dd1f2963d55cd5 to your computer and use it in GitHub Desktop.
function timeStamp() {
var now = new Date(),
date = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ],
time = [ now.getHours(), now.getMinutes(), now.getSeconds() ];
// Leftpad seconds with 0
for ( var i = 1; i < 3; i++ ) {
if ( time[i] < 10 ) {
time[i] = "0" + time[i];
}
}
// Return the formatted string
return date.join("/") + " " + time.join(":");
}
// Revisited in 2018
export default () => {
const now = new Date()
const date = [
now.getMonth() + 1,
now.getDate(),
now.getFullYear()
]
let time = [
now.getHours(),
now.getMinutes(),
now.getSeconds()
]
// Leftpad time units with 0
time = time.map(t => t < 10 ? '0' + t : t)
// Return the formatted string
return `${date.join('/')} ${time.join(':')}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment