@shencom/utils-date
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

@shencom/utils-date

日期时间处理工具

Install

pnpm add @shencom/utils

# or

pnpm add @shencom/utils-date

Basic Usage

import { FormatDate, FormatTime } from '@shencom/utils';
// import { FormatDate, FormatTime } from '@shencom/utils-date';

Methods

FormatDate

  • 说明: 日期格式化
  • 类型: (date?: number | Date | string, format?: string) => string
  • 参数:
    • date: 时间,默认: new Date()
    • format: 格式化规则,默认: YYYY-MM-DD
  • 示例:
    FormatDate(); // 2022-02-18
    FormatDate(new Date()); // 2022-02-18
    FormatDate(Date.now()); // 2022-02-18

FormatTime

  • 说明: 时间格式化
  • 类型: (date?: number | Date | string, format?: string) => string
  • 参数:
    • date: 时间,默认: new Date()
    • format: 格式化规则,默认: HH:mm:ss
  • 示例:
    FormatTime(); // 14:50:52
    FormatTime(new Date()); // 14:50:52
    FormatTime(Date.now()); // 14:50:52

FormatDateTime

  • 说明: 日期时间格式化
  • 类型: (date?: number | Date | string, format?: string) => string
  • 参数:
    • date: 时间,默认: new Date()
    • format: 格式化规则,默认: YYYY-MM-DD HH:mm:ss
  • 示例:
    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

FormatIntervalDate

  • 说明: 获取两个时间段的时间差
  • 类型: (start: number | string, end?: number | string | Date, unit?: dayjs.UnitType) => number
  • 参数:
    • start: 开始时间
    • end: 结束时间,默认: new Date()
    • unit: 返回单位类型,默认: minute
  • 示例:
    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

FormatSecond

  • 说明: 格式化秒 => 时分秒
  • 类型: (second: number, format?: string) => string
  • 参数:
    • second: 时间,默认: new Date()
    • format: 格式,默认: h:m:s
  • 示例:
    FormatSecond(1); // '0时00分01秒'
    FormatSecond(60); // '0时01分00秒'
    FormatSecond(60, 'm:s'); // '01分00秒'
    FormatSecond(3600); // '1时00分00秒'

Today

  • 说明: 返回今天日期
  • 类型: () => string
  • 示例:
    Today(); // '2022-12-08'

Yesterday

  • 说明: 返回昨天日期
  • 类型: () => string
  • 示例:
    Yesterday(); // '2022-12-07'

Tomorrow

  • 说明: 返回明天日期
  • 类型: () => string
  • 示例:
    Tomorrow(); // '2022-12-09'

PrevWeek

  • 说明: 返回上周(7 天前日期)
  • 类型: () => string
  • 示例:
    PrevWeek(); // '2022-12-01'

NextWeek

  • 说明: 返回下周(7 天后日期)
  • 类型: () => string
  • 示例:
    NextWeek(); // '2022-12-15'

PrevMonth

  • 说明: 返回上个月日期
  • 类型: () => string
  • 示例:
    PrevMonth(); // '2022-11-08'

NextMonth

  • 说明: 返回下个月日期
  • 类型: () => string
  • 示例:
    NextMonth(); // '2023-01-08'

PrevYear

  • 说明: 返回一年前日期
  • 类型: () => string
  • 示例:
    PrevYear(); // '2023-12-08'

NextYear

  • 说明: 返回一年后日期
  • 类型: () => string
  • 示例:
    NextYear(); // '2023-12-08'

IsAM

  • 说明: 当时间是否为上午
  • 类型: () => boolean
  • 示例:
    IsAM(); // false

IsPM

  • 说明: 当时间是否为下午
  • 类型: () => boolean
  • 示例:
    IsPM(); // true

IsLeapYear

  • 说明: 是否为闰年
  • 描述: 闰年 366 天,平年 365 天
  • 类型: (year: number) => boolean
  • 参数:
    • year: 年份
  • 示例:
    IsLeapYear(2000); // true
    IsLeapYear(2004); // true
    IsLeapYear(2100); // false
    IsLeapYear(2020); // true

GetWeek

  • 说明: 获得当前日期是周几
  • 描述: 闰年 366 天,平年 365 天
  • 类型: (date?: Date, format?: string): string
  • 参数:
    • date: 日期参数,默认当前日期
    • format: 周格式化结果:“d”:日, “dd”:周日, “ddd”:星期日;默认“ddd”
  • 示例:
    const date = new Date('2022-12-08');
    GetWeek(date, 'dd'); // '四'
    GetWeek(date, 'ddd'); // '周四'
    GetWeek(date, 'dddd'); // '星期四'

PastTime

  • 说明: 获得过去时间的字符串显示
  • 描述: 例如:刚刚,1 分钟前,1 小时前等
  • 类型: (date?: Date, format?: string): string
  • 参数:
    • date: 日期或日期字符串
    • lang: 字符串语言,zh 和 en,默认 zh *
  • 示例:
    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年前'

GetOverTime

  • 说明: 获得剩余时间的字符串显示
  • 描述: 例如:1 天 10 小时 20 分钟 30 秒
  • 类型: (date?: Date, format?: string): string
  • 参数:
    • date: 日期或日期字符串
  • 示例:
    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()); // '0天00时00分00秒'

Dayjs

Readme

Keywords

Package Sidebar

Install

npm i @shencom/utils-date

Weekly Downloads

2

Version

1.3.1

License

ISC

Unpacked Size

74.4 kB

Total Files

10

Last publish

Collaborators

  • shencom