120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
// 依赖来源 https://github.com/iamkun/dayjs
|
|
import dayjs from 'dayjs';
|
|
|
|
// 导入本地化语言并设为默认使用,只需要全局有引入就行
|
|
// import('dayjs/locale/zh-cn');
|
|
// dayjs.locale('zh-cn');
|
|
|
|
/**年 列如:2022 */
|
|
export const YYYY = 'YYYY';
|
|
|
|
/**年-月 列如:2022-12 */
|
|
export const YYYY_MM = 'YYYY-MM';
|
|
|
|
/**年-月-日 列如:2022-12-30 */
|
|
export const YYYY_MM_DD = 'YYYY-MM-DD';
|
|
|
|
/**年月日时分秒 列如:20221230010159 */
|
|
export const YYYYMMDDHHMMSS = 'YYYYMMDDHHmmss';
|
|
|
|
/**年-月-日 时:分:秒 列如:2022-12-30 01:01:59 */
|
|
export const YYYY_MM_DD_HH_MM_SS = 'YYYY-MM-DD HH:mm:ss';
|
|
|
|
/**特殊 列如:2025-04-28 08:00:46 GMT+08:00 */
|
|
export const YYYY_MM_DD_HH_MM_SSZ = 'YYYY-MM-DD HH:mm:ss [GMT]Z';
|
|
/**国际时间 列如:2022-12-30T01:01:59+08:00 */
|
|
export const RFC3339 = 'YYYY-MM-DDTHH:mm:ssZ';
|
|
/**国际时间 列如:Thu, Nov 14 2024 10:19 GMT+08:00 */
|
|
export const RFC822Z = 'ddd, MMM DD YYYY HH:mm [GMT]Z';
|
|
|
|
/**
|
|
* 格式时间字符串
|
|
* @param dateStr 时间字符串
|
|
* @param formatStr 时间格式 默认YYYY-MM-DD HH:mm:ss
|
|
* @returns Date对象
|
|
*/
|
|
export function parseStrToDate(
|
|
dateStr: string,
|
|
formatStr: string = YYYY_MM_DD_HH_MM_SS
|
|
): Date {
|
|
return dayjs(dateStr, formatStr).toDate();
|
|
}
|
|
|
|
/**
|
|
* 格式时间
|
|
* @param date 可转的Date对象
|
|
* @param formatStr 时间格式 默认YYYY-MM-DD HH:mm:ssZZ
|
|
* @returns 时间格式字符串
|
|
*/
|
|
export function parseDateToStr(
|
|
date: string | number | Date,
|
|
formatStr: string = YYYY_MM_DD_HH_MM_SSZ
|
|
): string {
|
|
return dayjs(date).format(formatStr);
|
|
}
|
|
|
|
/**
|
|
* 格式时间成日期路径
|
|
*
|
|
* 年/月 列如:2022/12
|
|
* @returns 时间格式字符串 YYYY/MM
|
|
*/
|
|
export function parseDatePath(date: number | Date = Date.now()): string {
|
|
return dayjs(date).format('YYYY/MM');
|
|
}
|
|
|
|
/**
|
|
* 判断两次时间差
|
|
* @param endDate 结束时间
|
|
* @param startDate 开始时间
|
|
* @param unit 单位
|
|
* @returns 单位数值
|
|
*/
|
|
export function diffValue(
|
|
endDate: string | number | Date,
|
|
startDate: string | number | Date,
|
|
unit: 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year'
|
|
): number {
|
|
const value = Math.ceil(dayjs(endDate).diff(startDate, unit));
|
|
if (Number.isNaN(value)) return 0;
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* 格式化秒值为 ?h?m?s
|
|
* @param seconds 2558 秒
|
|
* @returns 1h42m38s
|
|
*/
|
|
export function parseDuration(seconds: number | string) {
|
|
seconds = Number(seconds) || 0;
|
|
const duration = new Date(seconds * 1000);
|
|
const hours = duration.getUTCHours();
|
|
const minutes = duration.getUTCMinutes();
|
|
const secondsLeft = duration.getUTCSeconds();
|
|
if (+hours > 0) {
|
|
return `${hours}h${minutes}m${secondsLeft}s`;
|
|
}
|
|
if (+minutes > 0) {
|
|
return `${minutes}m${secondsLeft}s`;
|
|
}
|
|
if (+secondsLeft > 0) {
|
|
return `${secondsLeft}s`;
|
|
}
|
|
return `${seconds}s`;
|
|
}
|
|
|
|
/**
|
|
* 格式时间不带年份
|
|
*
|
|
* 年-月\n时:分 列如:10-13 \n 15:13
|
|
* @returns MM-DD\nHH:mm
|
|
*/
|
|
export function parseDateWithoutYear(date: string | number | Date) {
|
|
const day = dayjs(date);
|
|
const M: string = `${day.month() + 1}`.padStart(2, '0');
|
|
const D: string = `${day.date()}`.padStart(2, '0');
|
|
const H: string = `${day.hour()}`.padStart(2, '0');
|
|
const m: string = `${day.minute()}`.padStart(2, '0');
|
|
return `${M}-${D}\n${H}:${m}`;
|
|
}
|