2019-07-07 21:46:31 -07:00
|
|
|
const defaultLocaleStringFormats: {[index: string]: string} = {
|
|
|
|
'weekday': 'narrow',
|
|
|
|
'era': 'narrow',
|
|
|
|
'year': 'numeric',
|
|
|
|
'month': 'numeric',
|
|
|
|
'day': 'numeric',
|
|
|
|
'hour': 'numeric',
|
|
|
|
'minute': 'numeric',
|
|
|
|
'second': 'numeric',
|
2022-12-21 23:01:59 -08:00
|
|
|
'timeZoneName': 'short',
|
2019-07-07 21:46:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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)) {
|
2022-06-09 22:36:55 -07:00
|
|
|
return date.toLocaleString(window.navigator.language, { [kind]: option ? option : defaultLocaleStringFormats[kind] });
|
2019-07-07 21:46:31 -07:00
|
|
|
} else {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-24 09:36:48 -07:00
|
|
|
export function formatDateTimeString(date: Date, format: string): string {
|
2019-07-07 21:46:31 -07:00
|
|
|
return format
|
|
|
|
.replace(/yyyy/g, date.getFullYear().toString())
|
|
|
|
.replace(/yy/g, date.getFullYear().toString().slice(-2))
|
2022-06-09 22:36:55 -07:00
|
|
|
.replace(/MMMM/g, date.toLocaleString(window.navigator.language, { month: 'long' }))
|
|
|
|
.replace(/MMM/g, date.toLocaleString(window.navigator.language, { month: 'short' }))
|
2019-07-07 21:46:31 -07:00
|
|
|
.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 {
|
2019-07-07 21:55:22 -07:00
|
|
|
return format.replace(/\[(([^\[]|\[\])*)\]|(([yMdHhmst])\4{0,3})/g, (match: string, localeformat?: string, unused?, datetimeformat?: string) => {
|
2019-07-07 21:46:31 -07:00
|
|
|
if (localeformat) return formatLocaleString(date, localeformat);
|
|
|
|
if (datetimeformat) return formatDateTimeString(date, datetimeformat);
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
}
|