-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shrink size futher (thanks @adriantombu!), add JSDOC for better DX
- Loading branch information
Antoni Kepinski
committed
Aug 24, 2020
1 parent
ac2d767
commit 3c8aaac
Showing
1 changed file
with
14 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
/** | ||
* Use this API for simple, most common formatting. | ||
* @param {Date} date - Date object, which should be used. | ||
* @param {string} exp - String, which you want to format, for example: {yyyy}-{MM}-{dd} or Current time: {hh}:{mm}:{ss}. | ||
* @return {string} String with formatted date. | ||
*/ | ||
export const format = (date: Date, exp: string): string => exp.replace(/{.*?}/g, key => { | ||
switch (key) { | ||
case '{yyyy}': | ||
return `${date.getFullYear()}`; | ||
case '{yy}': | ||
return `${date.getFullYear()}`.slice(-2); | ||
case '{MM}': | ||
return (date.getMonth() + 1).toString().padStart(2, '0'); | ||
return `${(date.getMonth() + 1)}`.padStart(2, '0'); | ||
case '{dd}': | ||
return date.getDate().toString().padStart(2, '0'); | ||
return `${date.getDate()}`.padStart(2, '0'); | ||
case '{HH}': | ||
return date.getHours().toString().padStart(2, '0'); | ||
return `${date.getHours()}`.padStart(2, '0'); | ||
case '{mm}': | ||
return date.getMinutes().toString().padStart(2, '0'); | ||
return `${date.getMinutes()}`.padStart(2, '0'); | ||
case '{ss}': | ||
return date.getSeconds().toString().padStart(2, '0'); | ||
return `${date.getSeconds()}`.padStart(2, '0'); | ||
case '{SSS}': | ||
return `${date.getMilliseconds()}`.padStart(3, '0'); | ||
/* c8 ignore next 2 */ | ||
default: | ||
return ''; | ||
} | ||
}); | ||
|
||
/* c8 ignore next */ | ||
export {default as localeFormat} from './locale'; |