zoukankan      html  css  js  c++  java
  • Day.js :一个轻量的处理时间和日期的 JavaScript 库

    今天偶然看到一个关于日期很方便的使用方法Day.js

    下面说一下具体的使用方法:

    github下载地址:https://github.com/iamkun/dayjs

    也可以使用npm安装:

    npm install dayjs --save

     

    然后在文件中引入:

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

    如果不想安装,还可以直接在script标签中引入:

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

    下面是API文档地址:

    https://github.com/iamkun/dayjs/blob/master/docs/zh-cn/API-reference.md

    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().date()
      星期 : dayjs().day()
      时 : dayjs().hour()
      分 : dayjs().minute()
      秒 : dayjs().second()
      毫秒 : dayjs().millisecond()
    • 设置 dayjs().set('year',2017) dayjs().set('month',9)
    • 增加时间并返回一个新的 Dayjs() 对象 dayjs().add(7, 'day') dayjs().add(7, 'year')
    • 减少时间并返回一个新的 Dayjs() 对象 dayjs().subtract(7, 'year') dayjs().subtract(7, 'month')
    • 返回当前时间的开头时间的 Dayjs() 对象,如月份的第一天。 dayjs().startOf('year') dayjs().startOf('month')
    • 返回当前时间的末尾时间的 Dayjs() 对象,如月份的最后一天。 dayjs().endOf('month') dayjs().endOf('year')
    • 格式化 dayjs().format() dayjs().format('YYYY-MM-DD dddd HH:mm:ss.SSS A')
    • 时间差 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')
    • Unix 时间戳 (毫秒) dayjs().valueOf()
    • Unix 时间戳 (秒) dayjs().unix()
    • 返回月份的天数 dayjs().daysInMonth()
    • 返回原生的 Date 对象 dayjs().toDate()
    • 返回包含时间数值的数组 dayjs().toArray()
    • 当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串 dayjs().toJSON() //2018-06-08T02:44:30.599Z
    • 返回 ISO8601 格式的字符串 dayjs().toISOString() //2018-06-08T02:46:06.554Z
    • 返回包含时间数值的对象 dayjs().toObject()
    • 字符串 dayjs().toString()
    • 检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前 dayjs('2018-06-01').isBefore(dayjs('2018-06-02'))
    • 检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同 dayjs().isSame(dayjs())
    • 检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后 dayjs().isAfter(dayjs())
    FormatOutputDescription
    YY 18 两位数的年份
    YYYY 2018 四位数的年份
    M 1-12 月份,从 1 开始
    MM 01-12 月份,两位数
    MMM Jan-Dec 简写的月份名称
    MMMM January-December 完整的月份名称
    D 1-31 月份里的一天
    DD 01-31 月份里的一天,两位数
    d 0-6 一周中的一天,星期天是 0
    dd Su-Sa 最简写的一周中一天的名称
    ddd Sun-Sat 简写的一周中一天的名称
    dddd Sunday-Saturday 一周中一天的名称
    H 0-23 小时
    HH 00-23 小时,两位数
    m 0-59 分钟
    mm 00-59 分钟,两位数
    s 0-59
    ss 00-59 秒 两位数
    SSS 000-999 秒 三位数
    Z +5:00 UTC 的偏移量
    ZZ +0500 UTC 的偏移量,数字前面加上 0
    A AM PM
    a am pm
  • 相关阅读:
    DedeCMS用channelartlist调用顶级栏目及列表
    利用SQL语句替换织梦DedeCms数据库内容
    PHP 获取当前目录下的所有文件
    APP 商城功能
    left join , inner join 区别
    微信支付现金红包接口(转)
    微信红包发送规则
    PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)
    调用微信红包接口返回(转)
    一起发红包 微信平台红包接口调用教程(转)
  • 原文地址:https://www.cnblogs.com/mengzekun/p/12170434.html
Copyright © 2011-2022 走看看