Skip to content

Instantly share code, notes, and snippets.

@edgarberm
Created September 5, 2016 06:28
Show Gist options
  • Save edgarberm/78508f4261e2251e5e6d64129a4e9027 to your computer and use it in GitHub Desktop.
Save edgarberm/78508f4261e2251e5e6d64129a4e9027 to your computer and use it in GitHub Desktop.
Get formatted date
export const repeat = (str, times) => (new Array(times + 1)).join(str);
export const padStart = (num, maxLength, char = ' ') => repeat(char, maxLength - num.toString().length) + num;
export const formatTime = (time) => {
const h = padStart(time.getHours(), 2, '0');
const m = padStart(time.getMinutes(), 2, '0');
const s = padStart(time.getSeconds(), 2, '0');
const ms = padStart(time.getMilliseconds(), 3, '0');
return `${h}:${m}:${s}.${ms}`;
};
export const now = () => formatTime(new Date());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment