Skip to content

Instantly share code, notes, and snippets.

@literakl
Created April 18, 2021 11:07
Show Gist options
  • Save literakl/77a9ac82eb85002ad3c3890d73166412 to your computer and use it in GitHub Desktop.
Save literakl/77a9ac82eb85002ad3c3890d73166412 to your computer and use it in GitHub Desktop.
function timestamp() {
const currentDate = new Date();
const day = currentDate.getDate();
const month = currentDate.getMonth() + 1;
const year = currentDate.getFullYear();
const hours = currentDate.getHours();
const minutes = currentDate.getMinutes();
const seconds = currentDate.getSeconds();
let ms = currentDate.getMilliseconds();
if (ms < 10) {
ms = `00${ms}`;
} else if (ms < 100) {
ms = `0${ms}`;
}
return `${year}-${month < 10 ? `0${month}` : month}-${day < 10 ? `0${day}` : day} ${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}:${seconds < 10 ? `0${seconds}` : seconds}:${ms}`;
}
@literakl
Copy link
Author

literakl commented Apr 18, 2021

primitive solution to get a padded date time

2021-03-08 04:09:09:007
2021-03-28 14:38:23:170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment