UNPKG

toolset.io

Version:

前端开发常用工具集。

203 lines (202 loc) 6.41 kB
/** * 时间单位类型 */ type Unit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'; /** * 日期时间处理类(兼容原有API并增强) */ export declare class DateTime { private readonly date; /** * 构造函数 * @param date 可选日期参数,支持Date对象、时间戳或ISO字符串 * @throws 如果传入无效日期会抛出错误 */ constructor(date?: Date | string | number); /** * 创建DateTime实例(工厂方法) * @param date 可选日期参数 * @returns 新的DateTime实例 */ static create(date?: Date | string | number): DateTime; /** * 获取星期几(中国习惯:周一为1,周日为7) * @returns 1-7的数字 */ day(): number; /** * 格式化日期时间 * @param formatStr 格式化字符串,默认'YYYY-MM-DD HH:mm:ss' * @returns 格式化后的日期字符串 */ format(formatStr?: string): string; /** * 添加时间量 * @param value 要添加的数值 * @param unit 时间单位 * @returns 新的DateTime实例 */ add(value: number, unit: Unit): DateTime; /** * 减去时间量 * @param value 要减去的数值 * @param unit 时间单位 * @returns 新的DateTime实例 */ subtract(value: number, unit: Unit): DateTime; /** * 获取时间单位的开始时刻 * @param unit 时间单位 * @returns 新的DateTime实例 */ startOf(unit: Unit): DateTime; /** * 获取时间单位的结束时刻 * @param unit 时间单位 * @returns 新的DateTime实例 */ endOf(unit: Unit): DateTime; /** * 计算两个日期的时间差 * @param other 要比较的日期 * @param unit 时间单位,默认为毫秒 * @returns 时间差值 */ diff(other: DateTime | Date | string | number, unit?: Unit): number; /** * 获取周范围(带偏移量) * @param offset 周偏移量(0=本周,1=下周,-1=上周) * @param weekStartsOn 周起始日(1-7,默认1表示周一) * @returns [周开始时间, 周结束时间] */ getWeekRange(offset?: number, weekStartsOn?: number): [DateTime, DateTime]; /** * 获取月范围(带偏移量) * @param offset 月偏移量(0=本月,1=下月,-1=上月) * @returns [月开始时间, 月结束时间] */ getMonthRange(offset?: number): [DateTime, DateTime]; /** * 获取季度范围(带偏移量) * @param offset 季度偏移量(0=本季,1=下季,-1=上季) * @returns [季度开始时间, 季度结束时间] */ getQuarterRange(offset?: number): [DateTime, DateTime]; /** * 获取年范围(带偏移量) * @param offset 年偏移量(0=本年,1=明年,-1=去年) * @returns [年开始时间, 年结束时间] */ getYearRange(offset?: number): [DateTime, DateTime]; /** * 是否为工作日(周一至周五) * @returns 是否为工作日 */ isWeekday(): boolean; /** * 是否为周末(周六或周日) * @returns 是否为周末 */ isWeekend(): boolean; /** * 判断日期是否相同(可精确到指定单位) * @param other 要比较的日期 * @param unit 比较单位,默认为毫秒级 * @returns 是否相同 */ isSame(other: DateTime | Date | string, unit?: Unit): boolean; /** * 判断是否在某个日期之前 * @param other 要比较的日期 * @returns 是否在前 */ isBefore(other: Date | string | number): boolean; /** * 判断是否在某个日期之后 * @param other 要比较的日期 * @returns 是否在后 */ isAfter(other: Date | string | number): boolean; /** * 检查日期是否有效 * @returns 是否有效 */ isValid(): boolean; /** * 转换为原生Date对象 * @returns Date对象 */ toDate(): Date; /** * 转换为时间戳 * @returns 时间戳(毫秒) */ valueOf(): number; /** * 转换为ISO字符串 * @returns ISO格式字符串 */ toString(): string; /** * 获取当前月份的天数 * @private * @returns 天数 */ private daysInMonth; } /** * 格式化日期(快捷方法) * @param date 日期,默认为当前时间 * @param format 格式字符串,默认为'YYYY-MM-DD HH:mm:ss' * @returns 格式化后的字符串 */ export declare function formatDate(date?: Date | string | number, format?: string): string; /** * 获取当前周范围 * @param format 可选格式字符串 * @returns [周开始时间, 周结束时间] */ export declare function getCurrentWeek(format?: string): [string, string]; /** * 获取当前月范围 * @param format 可选格式字符串 * @returns [月开始时间, 月结束时间] */ export declare function getCurrentMonth(format?: string): [string, string]; /** * 生成日期范围数组 * @param start 开始日期 * @param end 结束日期 * @param format 格式字符串,默认为'YYYY-MM-DD' * @returns 日期字符串数组 */ export declare function getDateRange(start: Date | string | number, end: Date | string | number, format?: string): string[]; /** * 人性化显示时间差 * @param start 开始时间 * @param end 结束时间,默认为当前时间 * @returns 人性化字符串(如"3天前") */ export declare function humanizeDiff(start: Date | string | number, end?: Date | string | number): string; declare global { interface Date { /** * 判断是否在某个日期之前 * @param other 要比较的日期 * @returns 是否在前 */ isBefore(other: Date): boolean; /** * 判断是否在某个日期之后 * @param other 要比较的日期 * @returns 是否在后 */ isAfter(other: Date): boolean; } } export default DateTime; /** * 时间转换成相对时间 * @param timestamp */ export declare function timeAgo(timestamp: number): string;