【Day.js】一个轻量2KB的处理时间和日期的JavaScript 库,常用API记录与分享

关于时间的操作,一直在使用momentjs这个库。方便灵巧,功能强大。唯一的缺点:包大小为200K。精简的方案:如dayjsmiment

dayjs本身就是对标momentjs进行开发的:

Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js.

它的用法非常简单。

dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');

和momentjs用法基本一模一样的。dayjs的API和moment几乎一模一样,所以如果想要替换到现有的momentjs代码,直接替换为dayjs即可。而且 dayjs的大小: 2KB!!!

github地址

安装

NPM:

npm install dayjs --save
import dayjs from 'dayjs'
// 或者 CommonJS
// var dayjs = require('dayjs');
dayjs().format();

CDN:

<!-- 最新的压缩后的 JavaScript 文件 -->
<script src="https://unpkg.com/dayjs"></script>
<script>
  dayjs().format();
</script>

API中文文档

常用API参考:

  • 当前时间
    dayjs()
  • 时间字符串
    dayjs(‘2018-06-03’)
  • 时间戳
    dayjs(1528361259484)
  • Date 对象
    dayjs(new Date(2018,8,18))
  • 复制
    dayjs().clone()
  • 检测当前 Dayjs 对象是否是一个有效的时间
    dayjs().isValid()

取值赋值

  • 获取/设置
    年 : dayjs().year()

    月 : dayjs().month()

    季度 : dayjs().quarter()
    日 : dayjs().date()
    星期 : dayjs().day()
    时 : dayjs().hour()
    分 : dayjs().minute()
    秒 : dayjs().second()
    毫秒 : dayjs().millisecond()

  • 获取

    dayjs().get(‘year’)
    dayjs().get(‘month’)

  • 设置
    dayjs().set(‘year’,2017)
    dayjs().set(‘month’,9)

    所有可用单位列表

    单位缩写描述
    dateD日期
    dayd星期(星期日0,星期六6)
    monthM月份(0-11)
    yeary
    hourh小时
    minutem分钟
    seconds
    millisecondms毫秒

操作

  • 增加时间并返回一个新的 Dayjs() 对象
    dayjs().add(7, ‘day’)
    dayjs().add(7, ‘year’)

    所有可用单位列表

    单位缩写描述
    weekw
    dayd星期(星期日0,星期六6)
    monthM月份(0-11)
    quarterQ依赖QuarterOfYear插件
    yeary
    hourh小时
    minutem分钟
    seconds
    millisecondms毫秒
  • 减少时间并返回一个新的 Dayjs() 对象
    dayjs().subtract(7, ‘year’)
    dayjs().subtract(7, ‘month’)

  • 返回当前时间的开头时间的 Dayjs() 对象,如月份的第一天。
    dayjs().startOf(‘year’)
    dayjs().startOf(‘month’)

    所有可用单位列表

    单位缩写描述
    dateD天00:00
    dayd星期00:00
    monthM月第一天00:00
    quarterQ季度第一个月第一天00:00,依赖QuarterOfYear插件
    yeary1月1日00点
    weekw周第一天00:00
    isoWeek周(ISO)
    hourh00:00:00
    minutem00:00
    seconds00
    millisecondms0
  • 返回当前时间的末尾时间的 Dayjs() 对象,如月份的最后一天。
    dayjs().endOf(‘month’)
    dayjs().endOf(‘year’)

显示

  • 格式化
    dayjs().format()
    dayjs().format(‘YYYY-MM-DD dddd HH:mm:ss.SSS A’)

    FormatOutputDescription
    YY18年,两位数
    YYYY2018年,四位数
    M1-12月,从1开始
    MM01-12月,两位数字
    MMMJan-Dec月,英文缩写
    D1-31
    DD01-31日,两位数
    H0-2324小时
    HH00-2324小时,两位数
    h1-1212小时
    hh01-1212小时,两位数
    m0-59分钟
    mm00-59分钟,两位数
    s0-59
    ss00-59秒,两位数
    S0-9毫秒(百),一位数
    SS00-99毫秒(十),两位数
    SSS000-999毫秒,三位数
    Z-05:00UTC偏移
    ZZ-0500UTC偏移,两位数
    AAM / PM上/下午,大写
    aam / pm上/下午,小写
    Do1st… 31st月份的日期与序号
  • 时间差
    dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),‘years’)
    dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),‘day’)
    dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),‘hour’)

    单位缩写描述
    weekw
    dayd星期(星期日0,星期六6)
    monthM月份(0-11)
    quarterQ依赖QuarterOfYear插件
    yeary
    hourh小时
    minutem分钟
    seconds
    millisecondms毫秒
  • Unix 时间戳 (毫秒)
    dayjs().valueOf()

  • Unix 时间戳 (秒)
    dayjs().unix()

  • 返回月份的天数
    dayjs().daysInMonth()

  • 获取月天数

    dayjs().toDate()

    dayjs('2019-01-25').daysInMonth() // 31
    
  • 返回原生的 Date 对象
    dayjs().toDate()

  • 返回包含时间数值的数组
    dayjs().toArray()

    dayjs('2019-01-25').toArray() // [ 2019, 0, 25, 0, 0, 0, 0 ]
    
  • 当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串
    dayjs().toJSON() //2018-06-08T02:44:30.599Z

  • 返回 ISO8601 格式的字符串
    dayjs().toISOString() //2018-06-08T02:46:06.554Z

  • 返回包含时间数值的对象
    dayjs().toObject()

    dayjs('2019-01-25').toObject()
    /* { years: 2019,
         months: 0,
         date: 25,
         hours: 0,
         minutes: 0,
         seconds: 0,
         milliseconds: 0 } */
    
  • 字符串
    dayjs().toString()

查询

  • 检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前
    dayjs(‘2018-06-01’).isBefore(dayjs(‘2018-06-02’))
  • 检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同
    dayjs().isSame(dayjs())
  • 检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后
    dayjs().isAfter(dayjs())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值