time-formater 时间格式化插件

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

特定字符串所代表的含义

 TokenOutput
月份M1 2 ... 11 12
 MM01 02 ... 11 12
 MMM1月 2月 ... 11月 12月
 MMMM一月 二月 ... 十二月
季度Q1 2 3 4
日期D1 2 ... 30 31
 Do1日 2日 ... 30日 31日
 DD01 02 ... 30 31
星期d0 1 2 3 4 5 6
 dd日 一 二 三 四 五 六
 ddd周日 周一 ... 周五 周六
 dddd星期日 星期一 ... 星期五 星期六
年份YYYY1970 1971 ... 2029 2030
上午/下午A凌晨 早上 ... 下午 晚上
 a凌晨 早上 ... 下午 晚上
时刻H0 1 ... 22 23
 HH00 01 ... 22 23
 h1 2 ... 11 12
 hh01 02 ... 11 12
分钟m0 1 ... 58 59
 mm00 01 ... 58 59
s0 1 ... 58 59
 ss00 01 ... 58 59
毫秒S0 1 ... 8 9
 SS00 01 ... 98 99
 SSS000 001 ... 998 999
Unix 时间戳X1495357559853
Unix 时间戳 毫秒x1495357559853

时差 (以现在为基准)

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 。

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

Keywords

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值