2022-07-28 09:32:31 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
export interface Str2momentOptions {
|
|
|
|
gmt?: boolean;
|
|
|
|
picker?: 'year' | 'month' | 'week' | 'quarter';
|
|
|
|
utcOffset?: any;
|
2023-03-30 15:49:57 +00:00
|
|
|
utc?: boolean;
|
2022-07-28 09:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDefaultFormat = (props: any) => {
|
|
|
|
if (props.format) {
|
|
|
|
return props.format;
|
|
|
|
}
|
|
|
|
if (props.dateFormat) {
|
|
|
|
if (props['showTime']) {
|
|
|
|
return `${props.dateFormat} ${props.timeFormat || 'HH:mm:ss'}`;
|
|
|
|
}
|
|
|
|
return props.dateFormat;
|
|
|
|
}
|
|
|
|
if (props['picker'] === 'month') {
|
|
|
|
return 'YYYY-MM';
|
|
|
|
} else if (props['picker'] === 'quarter') {
|
|
|
|
return 'YYYY-\\QQ';
|
|
|
|
} else if (props['picker'] === 'year') {
|
|
|
|
return 'YYYY';
|
|
|
|
} else if (props['picker'] === 'week') {
|
|
|
|
return 'YYYY-wo';
|
|
|
|
}
|
|
|
|
return props['showTime'] ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD';
|
|
|
|
};
|
|
|
|
|
2023-03-30 15:49:57 +00:00
|
|
|
export const toGmt = (value: moment.Moment) => {
|
|
|
|
if (!value || !moment.isMoment(value)) {
|
2022-07-28 09:32:31 +00:00
|
|
|
return value;
|
|
|
|
}
|
2023-03-30 15:49:57 +00:00
|
|
|
return `${value.format('YYYY-MM-DD')}T${value.format('HH:mm:ss.SSS')}Z`;
|
2022-07-28 09:32:31 +00:00
|
|
|
};
|
|
|
|
|
2023-03-30 15:49:57 +00:00
|
|
|
export const toLocal = (value: moment.Moment) => {
|
2022-07-28 09:32:31 +00:00
|
|
|
if (!value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
2023-03-30 15:49:57 +00:00
|
|
|
return value.map((val) => val.startOf('second').toISOString());
|
2022-07-28 09:32:31 +00:00
|
|
|
}
|
|
|
|
if (moment.isMoment(value)) {
|
2023-03-30 15:49:57 +00:00
|
|
|
return value.startOf('second').toISOString();
|
2022-07-28 09:32:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const toMoment = (val: any, options?: Str2momentOptions) => {
|
|
|
|
if (!val) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const offset = options.utcOffset || -1 * new Date().getTimezoneOffset();
|
2023-03-30 15:49:57 +00:00
|
|
|
const { gmt, picker, utc = true } = options;
|
|
|
|
|
|
|
|
if (!utc) {
|
|
|
|
return moment(val);
|
|
|
|
}
|
|
|
|
|
2022-07-28 09:32:31 +00:00
|
|
|
if (moment.isMoment(val)) {
|
|
|
|
return val.utcOffset(offset);
|
|
|
|
}
|
|
|
|
if (gmt || picker) {
|
|
|
|
return moment(val).utcOffset(0);
|
|
|
|
}
|
|
|
|
return moment(val).utcOffset(offset);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const str2moment = (value?: string | string[], options: Str2momentOptions = {}): any => {
|
|
|
|
return Array.isArray(value)
|
|
|
|
? value.map((val) => {
|
|
|
|
return toMoment(val, options);
|
|
|
|
})
|
|
|
|
: value
|
|
|
|
? toMoment(value, options)
|
|
|
|
: value;
|
|
|
|
};
|
2023-02-16 08:48:00 +00:00
|
|
|
|
|
|
|
const toStringByPicker = (value, picker) => {
|
|
|
|
if (picker === 'year') {
|
|
|
|
return value.format('YYYY') + '-01-01T00:00:00.000Z';
|
|
|
|
}
|
|
|
|
if (picker === 'month') {
|
|
|
|
return value.format('YYYY-MM') + '-01T00:00:00.000Z';
|
|
|
|
}
|
|
|
|
if (picker === 'quarter') {
|
|
|
|
return value.format('YYYY-MM') + '-01T00:00:00.000Z';
|
|
|
|
}
|
|
|
|
if (picker === 'week') {
|
|
|
|
return value.format('YYYY-MM-DD') + 'T00:00:00.000Z';
|
|
|
|
}
|
|
|
|
return value.format('YYYY-MM-DD') + 'T00:00:00.000Z';
|
|
|
|
};
|
|
|
|
|
|
|
|
const toGmtByPicker = (value: moment.Moment | moment.Moment[], picker?: any) => {
|
|
|
|
if (!value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value.map((val) => toStringByPicker(val, picker));
|
|
|
|
}
|
|
|
|
if (moment.isMoment(value)) {
|
|
|
|
return toStringByPicker(value, picker);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface Moment2strOptions {
|
|
|
|
showTime?: boolean;
|
|
|
|
gmt?: boolean;
|
|
|
|
picker?: 'year' | 'month' | 'week' | 'quarter';
|
|
|
|
}
|
|
|
|
|
2023-03-30 15:49:57 +00:00
|
|
|
export const moment2str = (value?: moment.Moment, options: Moment2strOptions = {}) => {
|
2023-02-16 08:48:00 +00:00
|
|
|
const { showTime, gmt, picker } = options;
|
|
|
|
if (!value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (showTime) {
|
|
|
|
return gmt ? toGmt(value) : toLocal(value);
|
|
|
|
}
|
|
|
|
return toGmtByPicker(value, picker);
|
|
|
|
};
|