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

time-formater

v1.1.3

Published

在javascript中显示日期

Downloads

853

Readme

time-formater

不是 time-format[t]er

English

在javascript中显示日期。

使用方法

npm i -S time-formater
const time = require('time-formater')

let rawDate = time().format('YYYY-MM-DD HH:mm:ss') // 当前时间
console.log(rawDate) // 2017-05-21 15:19:34

解析

  • 当前时间
let now = time()

如果参数为空则获取当前系统时间。

  • 数字
let date = time(1495355143424)

如果传入参数为数字,则表示UTC时间戳。

  • 字符串
let date = time('2017-05-21')
// or
let date = time('2017-05-21 16:37:02')
// or
let date = time('2017-05-21 13:20:35+0800')
// or
let date = time('2017-05-21 19:02:59-08:00')
// or
let date = time('2017-05-21 12:38:49Z')

如果传入字符串,则必须符合ISO 8601格式。

  • 原生日期对象
let date = time(new Date())

传入参数也可以是一个原生的javascript日期对象。

显示

format(string)

let rawDate = time().format('YYYY-MM-DD') // 2017-05-21

特定字符串所代表的含义

| | Token | Output | | ----: | ------ | --------- | | 月份 | M | 1 2 ... 11 12 | | | MM | 01 02 ... 11 12 | | | MMM | 1月 2月 ... 11月 12月 | | | MMMM | 一月 二月 ... 十二月 | | 季度 | Q | 1 2 3 4 | | 日期 | D | 1 2 ... 30 31 | | | Do | 1日 2日 ... 30日 31日 | | | DD | 01 02 ... 30 31 | | 星期 | d | 0 1 2 3 4 5 6 | | | dd | 日 一 二 三 四 五 六 | | | ddd | 周日 周一 ... 周五 周六 | | | dddd | 星期日 星期一 ... 星期五 星期六 | | 年份 | YYYY | 1970 1971 ... 2029 2030 | | 上午/下午 | A | 凌晨 早上 ... 下午 晚上 | | | a | 凌晨 早上 ... 下午 晚上 | | 时刻 | H | 0 1 ... 22 23 | | | HH | 00 01 ... 22 23 | | | h | 1 2 ... 11 12 | | | hh | 01 02 ... 11 12 | | 分钟 | m | 0 1 ... 58 59 | | | mm | 00 01 ... 58 59 | | | s | 0 1 ... 58 59 | | | ss | 00 01 ... 58 59 | | 毫秒 | S | 0 1 ... 8 9 | | | SS | 00 01 ... 98 99 | | | SSS | 000 001 ... 998 999 | | Unix 时间戳 | X | 1495357559853 | | Unix 时间戳 毫秒 | x | 1495357559853 |

时差 (以现在为基准)

fromNow()

let fromNow = time('2017-01-01').fromNow()
console.log(fromNow) // 4个月前

倒计时

const time = require('time-formater')
let remain = 100000 // 10万秒
let countdown = time.countdown(remain)
let token = '剩余:d天H小时m分钟s秒'

// 浏览器
function step() {
    document.title = countdown.format(token) // 剩余:1天3小时46分钟40秒
    requestAnimationFrame(step)
}
step()

countdown(time)

  • time <number | string | Date> 类型为数字表示剩余的秒数,为Date实例或字符串(符合ISO 8601格式),表示结束的时间点。

返回倒计时的时间量。

format(token)

  • token <string> 用于指定输出格式。例:'剩余:d天H小时m分钟s秒' => "剩余:1天11小时4分钟38秒"。

| token | 描述 | | ------------ | ------------------------------------------------------------ | | d | 天数 | | H | 小时数 | | m | 分钟数 | | s | 秒数 | | S | 毫秒数 | | #<number> | 前缀,表示在前面填充零到指定宽度。 例:#3d 表示将天数填充到3个字符,001 。 |

将时间量格式化为字符串。