Last active
January 6, 2021 05:28
-
-
Save htdangkhoa/2d450bec3b938ae54bfe94cbb9847e7e to your computer and use it in GitHub Desktop.
Alternative moment.js
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
| { | |
| "months": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], | |
| "monthsShort": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], | |
| "weekdays": [ | |
| { "index": 0, "label": "Sunday" }, | |
| { "index": 1, "label": "Monday" }, | |
| { "index": 2, "label": "Tuesday" }, | |
| { "index": 3, "label": "Wednesday" }, | |
| { "index": 4, "label": "Thursday" }, | |
| { "index": 5, "label": "Friday" }, | |
| { "index": 6, "label": "Saturday" } | |
| ], | |
| "weekdaysShort": [ | |
| { "index": 0, "label": "Sun" }, | |
| { "index": 1, "label": "Mon" }, | |
| { "index": 2, "label": "Tue" }, | |
| { "index": 3, "label": "Wed" }, | |
| { "index": 4, "label": "Thu" }, | |
| { "index": 5, "label": "Fri" }, | |
| { "index": 6, "label": "Sat" } | |
| ], | |
| "weekdaysMin": [ | |
| { "index": 0, "label": "Su" }, | |
| { "index": 1, "label": "Mo" }, | |
| { "index": 2, "label": "Tu" }, | |
| { "index": 3, "label": "We" }, | |
| { "index": 4, "label": "Th" }, | |
| { "index": 5, "label": "Fr" }, | |
| { "index": 6, "label": "Sa" } | |
| ] | |
| } |
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
| const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g; | |
| const pad = (value) => { | |
| return `${value}`.padStart(2, '0'); | |
| }; | |
| const createOffset = (date) => { | |
| const sign = date.getTimezoneOffset() > 0 ? '-' : '+'; | |
| const offset = Math.abs(date.getTimezoneOffset()); | |
| const hours = pad(Math.floor(offset / 60)); | |
| const minutes = pad(offset % 60); | |
| return `${sign}${hours}:${minutes}`; | |
| }; | |
| const formatter = (date, format, schema) => { | |
| let calendarSchema = typeof schema === 'object' ? schema : require('./en.json'); | |
| const $date = new Date(date); | |
| const $Y = $date.getFullYear(); | |
| const $M = $date.getMonth(); | |
| const $D = $date.getDate(); | |
| const $W = $date.getDay(); | |
| const $H = $date.getHours(); | |
| const $h = $H % 12 === 0 ? 12 : $H % 12; | |
| const $m = $date.getMinutes(); | |
| const $s = $date.getSeconds(); | |
| const $ms = $date.getMilliseconds(); | |
| const matches = { | |
| YY: String($Y).slice(2), | |
| YYYY: String($Y), | |
| M: $M + 1, | |
| MM: pad($M + 1), | |
| MMM: `${(calendarSchema.monthsShort || [])[$M]}`, | |
| MMMM: `${(calendarSchema.months || [])[$M]}`, | |
| D: $D, | |
| DD: pad($D), | |
| d: String($W), | |
| dd: `${(calendarSchema.weekdaysMin || [])[$W].label}`, | |
| ddd: `${(calendarSchema.weekdaysShort || [])[$W].label}`, | |
| dddd: `${(calendarSchema.weekdays || [])[$W].label}`, | |
| H: String($H), | |
| HH: pad($H), | |
| h: String($h), | |
| hh: pad($h), | |
| a: $h >= 12 ? 'pm' : 'am', | |
| A: $h >= 12 ? 'PM' : 'AM', | |
| m: String($m), | |
| mm: pad($m), | |
| s: String($s), | |
| ss: pad($s), | |
| SSS: String($ms), | |
| Z: createOffset($date), | |
| ZZ: createOffset($date).replace(/\:/g, ''), | |
| }; | |
| return (format || 'YYYY-MM-DD[T]hh:mmZ').replace(REGEX_FORMAT, (match, $1) => $1 || matches[match]); | |
| }; | |
| export default formatter; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage