/** * 对日期时间进行量变处理 * * @example * //2020/5/1 08:00:20 * console.log(_.formatDate(_.addTime(new Date('2020-05-01'),20),'yyyy/MM/dd hh:mm:ss')) * //2020-04-11 08:00 * console.log(_.formatDate(_.addTime(new Date('2020-05-01'),-20,'d'))) * //2022-01-01 00:00 * console.log(_.formatDate(_.addTime(new Date('2020-05-01 0:0'),20,'M'))) * * @param date 原日期时间 * @param amount 变化量,可以为负数 * @param type 量变时间类型 * * @returns 日期对象 */ declare function addTime(date: Date | string | number, amount: number, type?: string): Date; /** * 比较两个日期,并返回由比较时间单位确定的相差时间。 *

* 使用truncated对比算法 —— 小于指定时间单位的值会被视为相同, * 比如对比月,则两个日期的 日/时/分/秒 会被认为相同,以此类推。 *

* 相差时间为正数表示date1日期晚于(大于)date2,负数相反,0表示时间/日期相同。 *

* 注意,如果对比单位是 h/m/s,务必要保持格式一致,比如 * * ```ts * //实际相差8小时 * new Date('2020-01-01') * //vs * new Date('2020/01/01') * ``` * * @example * //0 * console.log(_.compareDate(new Date('2020/05/01'),'2020/5/1')) * //格式不一致,相差8小时 * console.log(_.compareDate(new Date('2020-05-01'),'2020/5/1','h')) * //-59 * console.log(_.compareDate(new Date('2019/01/01'),'2019/3/1')) * * @param date1 日期对象、时间戳或合法格式的日期时间字符串。 * 对于字符串格式,可以时UTC格式,或者 * RFC2822格式 * @param date2 同date1 * @param type 比较时间单位 *

* @returns 根据比较时间单位返回的比较值。正数为date1日期晚于(大于)date2,负数相反,0表示相同。 */ declare function compareDate(date1: Date | string | number, date2: Date | string | number, type?: string): number; /** * 通过表达式格式化日期时间 * * ``` * yyyy-MM-dd hh:mm:ss => 2020-12-11 10:09:08 * ``` * * pattern解释: * * - `yy` 2位年 - 22 * - `yyyy` 4位年 - 2022 * - `M` 1位月(1-12) * - `MM` 2位月(01-12) * - `MMM` 月描述(一月 - 十二月) * - `d` 1位日(1-30/31/29/28) - `dd` 2位日(01-30/31/29/28) - `ddd` 一年中的日(1-365) - `dddd` 一年中的日(001-365) - `h` 1位小时(1-12) - `hh` 2位小时(01-12) - `H` 1位小时(0-23) - `HH` 2位小时(00-23) - `m` 1位分钟(0-59) - `mm` 2位分钟(00-59) - `s` 1位秒(0-59) - `ss` 2位秒(00-59) - `Q` 季度(1-4) - `QQ` 季度描述(春-冬) - `W` 一年中的周(1-53) - `WW` 一年中的周(01-53) - `w` 一月中的周(1-6) - `ww` 一月中的周描述(第一周 - 第六周) - `E` 星期(1-7) - `EE` 星期描述(星期一 - 星期日) - `S` 毫秒 - `a` AM/PM * * @example * //now time * console.log(_.formatDate(_.now(),'yyyy-MM-dd hh:mm')) * //2/1/2021 * console.log(_.formatDate('2021-2-1','M/d/yyyy')) * //2/1/21 * console.log(_.formatDate('2021-2-1','M/d/yy')) * //02/01/21 * console.log(_.formatDate('2021-2-1','MM/dd/yy')) * //02/01/2021 * console.log(_.formatDate('2021-2-1','MM/dd/yyyy')) * //21/02/01 * console.log(_.formatDate('2021-2-1','yy/MM/dd')) * //2021-02-01 * console.log(_.formatDate('2021-2-1','yyyy-MM-dd')) * //21-12-11 10:09:08 * console.log(_.formatDate('2021-12-11T10:09:08','yy-MM-dd HH:mm:ss')) * //12/11/2020 1009 * console.log(_.formatDate('2020-12-11 10:09:08','MM/dd/yyyy hhmm')) * //2020-12-11 08:00 * console.log(_.formatDate(1607644800000)) * //'' * console.log(_.formatDate('13:02')) * //'' * console.log(_.formatDate(null)) * //现在时间:(20-12-11 10:09:08) * console.log(_.formatDate('2020-12-11 10:09:08','现在时间:(yy-MM-dd hh:mm:ss)')) * * @param val 需要格式化的值,可以是日期对象或时间字符串或日期毫秒数 * @param pattern 格式化模式 * @returns 格式化后的日期字符串,无效日期返回空字符串 */ declare function formatDate(val: string | Date | number, pattern?: string): string; declare namespace formatDate { var locale: (lang: string, options: { quarters: string[]; months: string[]; weeks: string[]; days: string[]; meridiems: string[]; }) => void; var lang: (lang: string) => void; } /** * 获取指定日期在当前年中的天数并返回 * @param date 日期对象 * @returns 当前年中的第几天 */ declare function getDayOfYear(date: Date | string | number): number; /** * 获取指定日期在当前月中的周数并返回 * @param date 日期对象 * @returns 当前月中的第几周 */ declare function getWeekOfMonth(date: Date | string | number): number; /** * 获取指定日期在当前年中的周数并返回 * @param date 日期对象 * @returns 当前年中的第几周 */ declare function getWeekOfYear(date: Date | string | number): number; /** * 指定日期是否是闰年 * @param date 日期对象 * @returns 闰年返回true */ declare function isLeapYear(date: Date): boolean; /** * 比较两个日期是否为同一天 * @example * //true * console.log(_.isSameDay(new Date('2020-05-01'),'2020/5/1')) * //false * console.log(_.isSameDay(new Date('2020-05-01 23:59:59.999'),'2020/5/2 0:0:0.000')) * * @param date1 日期对象或合法格式的日期时间字符串 * @param date2 同date1 * @returns */ declare function isSameDay(date1: Date | string | number, date2: Date | string | number): boolean; /** * 返回13位日期毫秒数,表示从1970 年 1 月 1 日 00:00:00 (UTC)起到当前时间 * * @example * //now time * console.log(_.now()) * * @returns 带毫秒数的时间戳 */ declare function now(): number; /** * 通过指定参数得到日期对象。支持多种签名 * * ```js * _.toDate(1320940800); //timestamp unix style * _.toDate(1320940800123); //timestamp javascript style * _.toDate([year,month,day]); //注意,month的索引从1开始 * _.toDate([year,month,day,hour,min,sec]); //注意,month的索引从1开始 * _.toDate(datetimeStr); * ``` * * @example * //'2011/11/11 00:00:00' * console.log(_.toDate(1320940800).toLocaleString()) * //'2011/11/11 00:01:39' * console.log(_.toDate(1320940899999).toLocaleString()) * //'2022/12/12 00:00:00' * console.log(_.toDate([2022,11,12]).toLocaleString()) * //'2022/12/12 12:12:12' * console.log(_.toDate([2022,11,12,12,12,12]).toLocaleString()) * //'2022/2/2 00:00:00' * console.log(_.toDate('2022/2/2').toLocaleString()) * //'2022/2/2 08:00:00' * console.log(_.toDate('2022-02-02').toLocaleString()) * * @param value 转换参数 * * @returns 转换后的日期。无效日期统一返回1970/1/1 */ declare function toDate(value: number | [year: number, monthIndex: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number] | string | Date): Date; export { addTime, compareDate, formatDate, getDayOfYear, getWeekOfMonth, getWeekOfYear, isLeapYear, isSameDay, now, toDate };