npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shencom/utils-date

v1.3.2

Published

日期时间处理工具

Downloads

39

Readme

@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