diff options
| author | tamaina <tamaina@hotmail.co.jp> | 2019-07-08 13:46:31 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2019-07-08 13:46:31 +0900 |
| commit | 5343b005df9446c3cc13bdfb419db9b8720e5946 (patch) | |
| tree | bc8c8bdf0da2d3d9237eed2a4f14309c6c26c410 /src/misc | |
| parent | Better display in narrow desktop and choosing client type; Fix #1442, Fix #21... (diff) | |
| download | sharkey-5343b005df9446c3cc13bdfb419db9b8720e5946.tar.gz sharkey-5343b005df9446c3cc13bdfb419db9b8720e5946.tar.bz2 sharkey-5343b005df9446c3cc13bdfb419db9b8720e5946.zip | |
Improve paste uploading Resolve #3023 (#4542)
* resolve #3023
* fix
* fix
* better description
* widget
* fix text
* Update post-form.vue
* Fix enter-file-name dialog title text
* Fix type
* On messaging room
* Replace moment.js to original one
* Fix formatDateTimeString
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/format-time-string.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/misc/format-time-string.ts b/src/misc/format-time-string.ts new file mode 100644 index 0000000000..4729036e5b --- /dev/null +++ b/src/misc/format-time-string.ts @@ -0,0 +1,50 @@ +const defaultLocaleStringFormats: {[index: string]: string} = { + 'weekday': 'narrow', + 'era': 'narrow', + 'year': 'numeric', + 'month': 'numeric', + 'day': 'numeric', + 'hour': 'numeric', + 'minute': 'numeric', + 'second': 'numeric', + 'timeZoneName': 'short' +}; + +function formatLocaleString(date: Date, format: string): string { + return format.replace(/\{\{(\w+)(:(\w+))?\}\}/g, (match: string, kind: string, unused?, option?: string) => { + if (['weekday', 'era', 'year', 'month', 'day', 'hour', 'minute', 'second', 'timeZoneName'].includes(kind)) { + return date.toLocaleString(window.navigator.language, {[kind]: option ? option : defaultLocaleStringFormats[kind]}); + } else { + return match; + } + }); +} + +function formatDateTimeString(date: Date, format: string): string { + return format + .replace(/yyyy/g, date.getFullYear().toString()) + .replace(/yy/g, date.getFullYear().toString().slice(-2)) + .replace(/MMMM/g, date.toLocaleString(window.navigator.language, { month: 'long'})) + .replace(/MMM/g, date.toLocaleString(window.navigator.language, { month: 'short'})) + .replace(/MM/g, (`0${date.getMonth() + 1}`).slice(-2)) + .replace(/M/g, (date.getMonth() + 1).toString()) + .replace(/dd/g, (`0${date.getDate()}`).slice(-2)) + .replace(/d/g, date.getDate().toString()) + .replace(/HH/g, (`0${date.getHours()}`).slice(-2)) + .replace(/H/g, date.getHours().toString()) + .replace(/hh/g, (`0${(date.getHours() % 12) || 12}`).slice(-2)) + .replace(/h/g, ((date.getHours() % 12) || 12).toString()) + .replace(/mm/g, (`0${date.getMinutes()}`).slice(-2)) + .replace(/m/g, date.getMinutes().toString()) + .replace(/ss/g, (`0${date.getSeconds()}`).slice(-2)) + .replace(/s/g, date.getSeconds().toString()) + .replace(/tt/g, date.getHours() >= 12 ? 'PM' : 'AM'); +} + +export function formatTimeString(date: Date, format: string): string { + return format.replace(/\[(([^\[]|\[\])*)\]|([yMdHhmst]{1,4})/g, (match: string, localeformat?: string, unused?, datetimeformat?: string) => { + if (localeformat) return formatLocaleString(date, localeformat); + if (datetimeformat) return formatDateTimeString(date, datetimeformat); + return match; + }); +} |