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

@gaopeng123/utils.date

v1.1.18-alpha.1

Published

时间数据处理

Downloads

361

Readme

date

时间戳格式化

formatTimestamp

(timestamp: Date | number | string, type: TimestampType = 'yyyy-MM-dd HH:mm:ss'): string

时间戳转换成格式化后的日期格式

export type TimestampType = 'yyyy-MM-dd HH:mm:ss' | 'yyyy/MM/dd HH:mm:ss'
    | 'yyyy-MM-dd HH:mm:ss WW' | 'yyyy/MM/dd HH:mm:ss WW'
    | 'yyyy-MM-dd' | 'yyyy/MM/dd'
    | 'HH:mm:ss'
    | 'MM-dd' | 'MM/dd'
    | 'MM-dd HH:mm:ss' | 'MM/dd HH:mm:ss'
    | 'dd HH:mm:ss'
    | 'yyyy' | 'MM' | 'dd' | 'HH' | 'mm' | 'ss' | 'WW' | 'ww' | string

timestampToDate(Date.now(), 'yyyy-MM-dd')
timestampToDate(new Date(), 'yyyy-MM-dd')
timestampToDate((new Date()).toString(), 'yyyy-MM-dd')

formatTimestamp(1647495559126))// '2022-03-17 13:39:19'
formatTimestamp(1647495559126, 'MM-dd HH:mm:ss'))// '03-17 13:39:19'
formatTimestamp(1647495559126, 'yyyy/MM/dd HH:mm:ss'))// '2022/03/17 13:39:19'
formatTimestamp(1647495559126, 'yyyy-MM-dd HH:mm:ss WWW'))// '2022-03-17 13:39:19 星期四'
formatTimestamp(1647495559126, 'yyyy-MM-dd HH:mm:ss 周WW'))// '2022-03-17 13:39:19 周四'
formatTimestamp(1647495559126, '周WW yyyy-MM-dd HH:mm:ss'))// '周四 2022-03-17 13:39:19'
formatTimestamp(1647495559126, '周ww yyyy-MM-dd hh:mm:ss'))// '周4 2022-03-17 01:39:19'
timestampToCN

(timestamp: Date | number | string, type: TimeType = 'dd天HH小时mm分钟ss秒'): string

将毫秒转换成可读性中文显示

type TimeType = 'd天HH小时mm分钟ss秒' | string;

expect(timestampToCN(3600)).toEqual("3.6秒");
expect(timestampToCN(3601 * 1000)).toEqual("01小时00分钟01秒");
expect(timestampToCN(86400 * 1000 + 1000)).toEqual("01天00小时00分钟01秒");
expect(timestampToCN(86400 * 1000 + 1000, "d天H小时m分钟ss秒")).toEqual("1天0小时0分钟01秒");
expect(timestampToCN(3601 * 1000000)).toEqual("41天16小时16分钟40秒");

日期获取

getWeek

获取数字周几

getWeekCN(new Date());// 5  1-7
getWeekCN (date: Date): string

获取中文星期几

getWeekCN(new Date());// '星期五'
getWeekCNDay

获取是中文的周几

getWeekCN(new Date());// '五'
getMonthDays

根据时间戳获取其月份的天数

getMonthDays('2023-01-01'); // 31
getMonthDays(1672531200000); // 31
currentMonthDays

获取当月的天数

currentMonthDays(); // 31
currentYearMonthDays

获取本年任意月份的天数

currentYearMonthDays(1); // 31
currentYearMonthDays('02'); // 28
currentYearMonthDays('01');// 31

区间时间戳获取

timeRange: (days: number): TimeRange

获取{n}天的时间戳range

type TimeRange = {
	startTime: number;
	endTime: number;
};
timeRange(1);
timeRangeCurrent:(type: TimeRangeType): TimeRange

获取当前自然天、周、月、季度、年时间戳范围

type TimeRangeType = 'day' | 'threeDays' | 'week' | 'month' | 'quarter' | 'year';
timeRangeCurrent('week');
timeRangePrevious:(type: TimeRangeType): TimeRange

获取过去一天、一周、一月、一季度、一年的时间戳范围

timeRangePrevious('threeDays');
timeRangePeriod

获取过去{n}天/月的时间区间

timeRangePeriod('day', 4);
[
  {
    startTime: 1697126400000,
    endTime: 1697212799000,
    startTimeStr: '2023-10-13 00:00:00',
    endTimeStr: '2023-10-13 23:59:59'
  },
  {
    startTime: 1697212800000,
    endTime: 1697299199000,
    startTimeStr: '2023-10-14 00:00:00',
    endTimeStr: '2023-10-14 23:59:59'
  },
  {
    startTime: 1697299200000,
    endTime: 1697385599000,
    startTimeStr: '2023-10-15 00:00:00',
    endTimeStr: '2023-10-15 23:59:59'
  },
  {
    startTime: 1697385600000,
    endTime: 1697471999000,
    startTimeStr: '2023-10-16 00:00:00',
    endTimeStr: '2023-10-16 23:59:59'
  }
]
timeRangePeriod('mouth', 4);
[
  {
    startTime: 1688140800000,
    endTime: 1690819199000,
    startTimeStr: '2023-07-01 00:00:00',
    endTimeStr: '2023-07-31 23:59:59'
  },
  {
    startTime: 1690819200000,
    endTime: 1693497599000,
    startTimeStr: '2023-08-01 00:00:00',
    endTimeStr: '2023-08-31 23:59:59'
  },
  {
    startTime: 1693497600000,
    endTime: 1696089599000,
    startTimeStr: '2023-09-01 00:00:00',
    endTimeStr: '2023-09-30 23:59:59'
  },
  {
    startTime: 1696089600000,
    endTime: 1698767999000,
    startTimeStr: '2023-10-01 00:00:00',
    endTimeStr: '2023-10-31 23:59:59'
  }
]

自然时间戳获取

currentQuarterFirstMonth

本季度的第一个月份

currentQuarterFirstMonth(); // number
currentQuarterLastMonth

本季度的最后一个月份

currentQuarterLastMonth(); // number
currentQuarterDays

本季度有多少天

currentQuarterDays(); // number
currentDayEarliest

当天0点时间戳

currentDayEarliest(); // number
currentDayLatest

当天24点时间戳

currentDayLatest(); // number