Skip to content

Instantly share code, notes, and snippets.

@k8scat
Created April 29, 2021 04:31
Show Gist options
  • Save k8scat/2b43041bacdabc5aaa90acfbecd8d8e1 to your computer and use it in GitHub Desktop.
Save k8scat/2b43041bacdabc5aaa90acfbecd8d8e1 to your computer and use it in GitHub Desktop.
Format time from timestamp
export const formatTime = (timestamp) => {
const date = new Date(timestamp);
const YY = date.getFullYear();
const MM = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
const DD = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
const hh = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
const mm = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
const ss = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
return `${YY}-${MM}-${DD} ${hh}:${mm}:${ss}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment