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

date-booster

v1.0.3

Published

Date booster

Downloads

2

Readme

介绍

处理时间的工具库。

下载

npm

npm i date-booster

src

<script src="../dist/date-booster.js"></script>

使用

// import DateBooster from 'date-booster'

const dateBooster = new DateBooster()

API

foldDate

/**
 * 将属于同一时间范围的数据折叠到一起。
 *
 * @param {Array} data - 待处理的数据,数组的每一项是对象,对象中通过 date 属性标识时间
 * @param {string} [rule=day] - 折叠方式,可选值有 hour | day | month | year
 * @param {boolean} [descending=true] - 时间降序排列
 * @return {array} 处理后的数据
 */
const data = [
  {
    number: 1,
    date: new Date('2022-06-01T09:00')
  },
  {
    number: 2,
    date: new Date('2022-06-02T12:00'),
  },
  {
    number: 3,
    date: new Date('2022-06-01T08:00')
  },
  {
    number: 4,
    date: new Date('2022-06-03T06:00'),
  },
  {
    number: 5,
    date: new Date('2022-06-02T07:00'),
  },
  {
    number: 6,
    date: new Date('2022-06-02T07:10')
  }
]

dateBooster.foldDate(data, 'day')
// output
[
  [
    {
      number: 4,
      date: Fri Jun 03 2022 06:00:00 GMT+0800 (中国标准时间) {}
    }
  ],
  [
    {
      number: 5,
      date: Thu Jun 02 2022 07:00:00 GMT+0800 (中国标准时间) {}
    },
    {
      number: 6,
      date: Thu Jun 02 2022 07:10:00 GMT+0800 (中国标准时间) {}
    },
    {
      number: 2,
      date: Thu Jun 02 2022 12:00:00 GMT+0800 (中国标准时间) {}
    }
  ],
  [
    {
      number: 3,
      date: Wed Jun 01 2022 08:00:00 GMT+0800 (中国标准时间) {}
    },
    {
      number: 1,
      date: Wed Jun 01 2022 09:00:00 GMT+0800 (中国标准时间) {}
    }
  ]
]

getMonth

/**
 * 获取当前日期的月份
 * 
 * @param {(Date|string)} data - 当前日期
 * @param {boolean} [shorthand=trye] - 是否简写
 * @return {string} 当前日期的月份
 */
dateBooster.getMonth('2022-06-29T12:00') // output: Jun
dateBooster.getMonth('2022-06-29T12:00', false) // output: June

getWeek

/**
 * 获取当前日期的星期
 * 
 * @param {(Date|string)} data - 当前日期
 * @param {boolean} [shorthand=true] - 是否简写
 * @return {string} 当前日期的星期
 */
dateBooster.getWeek('2022-06-29T12:00') // output: Wed
dateBooster.getWeek('2022-06-29T12:00', false) // output: Wednesday

getTimeDiff

/**
 * 计算两个日期间的时间差
 *
 * @param {(Date|string)} date1 - 开始时间
 * @param {(Date|string)} date2 - 结束时间
 * @param {string} format - 时间差值的单位,可选值有: s(秒) | m(分) | h(时) | d(天)
 * @return {number} 时间差值
 */
dateBooster.getTimeDiff('2022-06-30T12:00', '2022-06-30T13:45', 'm') // output: 105
dateBooster.getTimeDiff('2022-06-30T12:00', '2022-07-15T11:45', 'd') // output: 14

countdown

/**
 * 倒计时
 *
 * @param {HTMLElement} - 显示倒计时的元素
 * @param {(Date|string)} - 倒计时为0的时刻
 */
dateBooster.countdown(elm, '2022-06-30T15:11') // elm.innerText: 00:54:38