Created
September 5, 2016 06:28
-
-
Save edgarberm/78508f4261e2251e5e6d64129a4e9027 to your computer and use it in GitHub Desktop.
Get formatted date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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