refactor(datetime-format): integrate dayjs timezone support with default Moscow timezone for date and time formatting
This commit is contained in:
parent
e8771ed999
commit
6a1565825d
@ -1,9 +1,19 @@
|
||||
/* eslint-disable import/no-unassigned-import */
|
||||
import { isBrowser } from './environment';
|
||||
import 'dayjs/locale/ru';
|
||||
import dayjs from 'dayjs';
|
||||
import timezone from 'dayjs/plugin/timezone';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
import 'dayjs/locale/ru';
|
||||
import { noop } from 'radashi';
|
||||
|
||||
// Подключаем плагины один раз
|
||||
if (!dayjs.prototype.tz) {
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
}
|
||||
|
||||
// Europe/Moscow как дефолтный часовой пояс
|
||||
export const MOSCOW_TZ = 'Europe/Moscow';
|
||||
|
||||
export function combineDateTime(date: Date, time: string) {
|
||||
const [hours = '00', minutes = '00'] = time.split(':');
|
||||
|
||||
@ -20,23 +30,32 @@ export function formatDate(date: Date | string) {
|
||||
if (!date) return { db: noop, user: noop };
|
||||
|
||||
return {
|
||||
db: () => dayjs(date).format('YYYY-MM-DD'),
|
||||
user: (template?: string, fallbackLang: string = 'ru') => {
|
||||
const lang = isBrowser() ? document.documentElement.lang || fallbackLang : fallbackLang;
|
||||
|
||||
dayjs.locale(lang);
|
||||
|
||||
return dayjs(date).format(template || 'D MMMM YYYY');
|
||||
db: (tz: string = MOSCOW_TZ) => {
|
||||
// date — локальная дата в tz
|
||||
const local = dayjs.tz(date, tz);
|
||||
return local.utc().format('YYYY-MM-DD');
|
||||
},
|
||||
user: (template?: string, tz: string = MOSCOW_TZ) => {
|
||||
return dayjs
|
||||
.utc(date)
|
||||
.tz(tz)
|
||||
.format(template || 'D MMMM YYYY');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function formatTime(time: string) {
|
||||
const [hours = '00', minutes = '00'] = time.split(':');
|
||||
|
||||
return {
|
||||
db: () => `${hours}:${minutes}:00`,
|
||||
user: () => `${hours}:${minutes}`,
|
||||
// time — локальное время в tz (по умолчанию Europe/Moscow)
|
||||
db: (tz: string = MOSCOW_TZ) => {
|
||||
const local = dayjs.tz(`1970-01-01T${hours}:${minutes}:00`, tz);
|
||||
return local.utc().format('HH:mm:00');
|
||||
},
|
||||
user: (tz: string = MOSCOW_TZ) => {
|
||||
const utcTime = dayjs.utc(`1970-01-01T${hours}:${minutes}:00Z`);
|
||||
return utcTime.tz(tz).format('HH:mm');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user