@shencom/utils-date
Version:
243 lines (214 loc) • 6.01 kB
text/typescript
import dayjs from 'dayjs';
import {
_dayTime,
_hourTime,
_minuteTime,
_monthTime,
_yearTime,
pastTimeTypes,
week,
} from './constant';
import { FormatDate, FormatSecond } from './format';
export * from './format';
export * from './range-constant';
/**
* 返回今天日期
* @example
* ```ts
* Today(); // '2022-12-08'
* ```
* @returns {String}
*/
export const Today = () => FormatDate();
/**
* 返回昨天日期
* @example
* ```ts
* Yesterday(); // '2022-12-07'
* ```
* @returns {String}
*/
export const Yesterday = () => FormatDate(dayjs().subtract(1, 'day').valueOf());
/**
* 返回明天日期
* @example
* ```ts
* Tomorrow(); // '2022-12-09'
* ```
* @returns {String}
*/
export const Tomorrow = () => FormatDate(dayjs().add(1, 'day').valueOf());
/**
* 返回上周(7天前日期)
* @example
* ```ts
* PrevWeek(); // '2022-12-01'
* ```
* @returns {String}
*/
export const PrevWeek = () => FormatDate(dayjs().subtract(1, 'week').valueOf());
/**
* 返回下周(7天后日期)
* @example
* ```ts
* NextWeek(); // '2022-12-15'
* ```
* @returns {String}
*/
export const NextWeek = () => FormatDate(dayjs().add(1, 'week').valueOf());
/**
* 返回上个月日期
* @example
* ```ts
* PrevMonth(); // '2022-11-08'
* ```
* @returns {String}
*/
export const PrevMonth = () => FormatDate(dayjs().subtract(1, 'month').valueOf());
/**
* 返回下个月日期
* @example
* ```ts
* NextMonth(); // '2023-01-08'
* ```
* @returns {String}
*/
export const NextMonth = () => FormatDate(dayjs().add(1, 'month').valueOf());
/**
* 返回一年前日期
* @example
* ```ts
* PrevYear(); // '2020-12-08'
* ```
* @returns {String}
*/
export const PrevYear = () => FormatDate(dayjs().subtract(1, 'year').valueOf());
/**
* 返回一年后日期
* @example
* ```ts
* NextYear(); // '2023-12-08'
* ```
* @returns {String}
*/
export const NextYear = () => FormatDate(dayjs().add(1, 'year').valueOf());
/**
* 当时间是否为上午
* @example
* ```ts
* IsAM(); // false
* ```
* @returns {Boolean}
*/
export const IsAM = () => dayjs().format('A') === 'AM';
/**
* 当时间是否为下午
* @example
* ```ts
* IsPM(); // true
* ```
* @returns {Boolean}
*/
export const IsPM = () => dayjs().format('A') === 'PM';
/**
* 是否为闰年
* @description 闰年366天,平年365天
* @param {Number} year 年份
* @example
* ```ts
* IsLeapYear(2000); // true
* IsLeapYear(2004); // true
* IsLeapYear(2100); // false
* IsLeapYear(2020); // true
* ```
* @returns {Boolean}
*/
export const IsLeapYear = (year: number) =>
(year % 100 !== 0 && year % 4 === 0) || year % 400 === 0;
/**
* 获得当前日期是周几
* @param {Date} date 日期参数,默认当前日期
* @param {String} format 周格式化结果:“d”:日, “dd”:周日, “ddd”:星期日;默认“ddd”
* @example
* ```ts
* const date = new Date('2022-12-08');
* GetWeek(date, 'dd'); // '四'
* GetWeek(date, 'ddd'); // '周四'
* GetWeek(date, 'dddd'); // '星期四'
* ```
* @returns {String}
*/
export function GetWeek(date = new Date(), format = 'ddd') {
if (format === 'dd') return week[date.getDay()];
if (format === 'ddd') return `周${week[date.getDay()]}`;
if (format === 'dddd') return `星期${week[date.getDay()]}`;
throw new Error('Invalid week format!');
}
/**
* 获得过去时间的字符串显示
* @description 例如:刚刚,1分钟前,1小时前等
* @param {Date|String} date 日期或日期字符串
* @param {String} lang 字符串语言,zh和en,默认zh *
* @example
* ```ts
* PastTime(new Date(Date.now() - 1000 * 2)); // '刚刚'
* PastTime(Dayjs().subtract(12, 'hour').valueOf()); // '12小时前'
* PastTime(Dayjs().subtract(1, 'day').valueOf()); // '1天前'
* PastTime(Dayjs().subtract(1, 'month').valueOf()); // '1个月前'
* PastTime(Dayjs().subtract(1, 'year').valueOf()); // '1年前'
* ```
* @returns {String}
*/
export const PastTime = (val: number | string | Date) => {
const date = dayjs(val);
if (!date.isValid()) return '--';
// 计算时间差
const startTime = date.valueOf();
const currentTime = Date.now();
const time = currentTime - startTime;
// 年月日时分
const year = Math.floor(time / _yearTime);
const month = Math.floor(time / _monthTime);
const day = Math.floor(time / _dayTime);
const hour = Math.floor(time / _hourTime);
const minute = Math.floor(time / _minuteTime);
// 返回结果
if (year) return year + pastTimeTypes.year;
if (month) return month + pastTimeTypes.month;
if (day) return day + pastTimeTypes.day;
if (hour) return hour + pastTimeTypes.hour;
if (minute) return minute + pastTimeTypes.minute;
else return pastTimeTypes.just;
};
/**
* 获得剩余时间的字符串显示
* @description 例如:1天10小时20分钟30秒
* @param {Date|String} date 日期或日期字符串
* @example
* ```ts
* GetOverTime(Dayjs().valueOf()); // '0天00时00分00秒';
* GetOverTime(Dayjs().add(60, 'second').valueOf()); // '0天00时01分00秒';
* GetOverTime(Dayjs().add(1, 'second').valueOf()); // '0天00时00分01秒';
* GetOverTime(Dayjs().add(1, 'hour').valueOf()); // '0天01时00分00秒';
* GetOverTime(Dayjs().add(25, 'hour').valueOf()); // '1天01时00分00秒';
* GetOverTime(Dayjs().add(1, 'day').valueOf()); // '1天00时00分00秒';
* GetOverTime(Dayjs().add(1, 'week').valueOf()); // '7天00时00分00秒';
* GetOverTime(Dayjs().subtract(1, 'day').valueOf()); // '--'
* ```
* @returns {String}
*/
export const GetOverTime = (val: number | string | Date) => {
const date = dayjs(val);
if (!date.isValid()) return '--';
const endDate = Math.floor(date.valueOf() / 1000); // 结束时间
const startDate = Math.floor(Date.now() / 1000); // 开始时间
let t = endDate - startDate;
if (t <= 0) t = 0;
return FormatSecond(t, 'd:h:m:s');
};
/**
* 日期转化
*
* 参考 https://github.com/iamkun/dayjs
*/
export const Dayjs = dayjs;