@shencom/utils-date
Version:
122 lines (108 loc) • 3.55 kB
text/typescript
import dayjs from 'dayjs';
import { _dayTime, _hourTime, _minuteTime, _secondTime } from './constant';
export type IFormatDate = (date?: dayjs.Dayjs | number | Date | string, format?: string) => string;
const _digit = (val: number) => (val < 10 ? `0${val}` : val);
/**
* 日期格式化
*
* @param {(number | Date | string)} [date] 时间,默认: `new Date()`
* @param {string} [format] 格式化规则,默认: `YYYY-MM-DD`
* @example
* ```ts
* FormatDate(); // 2022-02-18
* FormatDate(new Date()); // 2022-02-18
* FormatDate(Date.now()); // 2022-02-18
* ```
* @returns {string}
*/
export const FormatDate: IFormatDate = (date = new Date(), format = 'YYYY-MM-DD') =>
dayjs(date).format(format);
/**
* 时间格式化
*
* @param {(number | Date | string)} [date] 时间,默认: `new Date()`
* @param {string} [format] 格式化规则,默认: `HH:mm:ss`
* @example
* ```ts
* FormatTime(); // 14:50:52
* FormatTime(new Date()); // 14:50:52
* FormatTime(Date.now()); // 14:50:52
* ```
* @returns {string}
*/
export const FormatTime: IFormatDate = (date, format = 'HH:mm:ss') => dayjs(date).format(format);
/**
* 日期时间格式化
*
* @param {(number | Date | string)} [date] 时间,默认: `new Date()`
* @param {string} [format] 格式化规则,默认: `YYYY-MM-DD HH:mm:ss`
* @example
* ```ts
* FormatDateTime(); // 2022-02-18 14:50:52
* FormatDateTime(new Date()); // 2022-02-18 14:50:52
* FormatDateTime(Date.now()); // 2022-02-18 14:50:52
* ```
* @returns {string}
*/
export const FormatDateTime: IFormatDate = (date, format = 'YYYY-MM-DD HH:mm:ss') =>
FormatDate(date, format);
/**
* 获取两个时间段的时间差
*
* @param {number} startDate 开始时间
* @param {number} [endDate] 结束时间,默认: `new Date()`
* @param {dayjs.UnitType} [unit] 返回单位类型,默认: `minute`
* @example
* ```ts
* FormatIntervalDate(1574254800000,1574251200000);// 60
* FormatIntervalDate('2022-02-18 16:00:00', '2022-02-18 17:00:00');// 60
* FormatIntervalDate('2022-02-18 17:00:00', '2022-02-18 16:00:00');// 60
* FormatIntervalDate('2022-02-18 16:00:00', '2022-02-18 16:10:00', 's');// 600
* ```
* @returns {number}
*/
export const FormatIntervalDate = (
start: number | string,
end: number | string | Date = new Date(),
unit: dayjs.UnitType = 'minute',
): number => Math.abs(dayjs(end).diff(dayjs(start), unit));
/**
* 格式化秒 => 时分秒
*
* @param {number} time 秒
* @param {string} [format] 格式,默认: `h:m:s`
* @example
* ```ts
* FormatSecond(1);// '0时00分01秒'
* FormatSecond(60);// '0时01分00秒'
* FormatSecond(60, 'm:s');// '01分00秒'
* FormatSecond(3600);// '1时00分00秒'
* ```
* @return {string}
*/
export const FormatSecond = (time: number, format = 'h:m:s') => {
if (!Number(time) || isNaN(time)) time = 0;
time = time * _secondTime;
const arr = format.split(':');
let timeStr = '';
const day = Math.floor(time / _dayTime);
if (arr.includes('d')) {
timeStr += `${day}天`;
if (day) time = time - day * _dayTime;
}
const hour = Math.floor(time / _hourTime);
if (arr.includes('h')) {
timeStr += `${_digit(hour)}时`;
if (hour) time = time - hour * _hourTime;
}
const minute = Math.floor(time / _minuteTime);
if (arr.includes('m')) {
timeStr += `${_digit(minute)}分`;
if (minute) time = time - minute * _minuteTime;
}
const second = Math.floor(time / _secondTime);
if (arr.includes('s')) {
timeStr += `${_digit(second)}秒`;
}
return timeStr;
};