zoukankan      html  css  js  c++  java
  • dayjs笔记

    1、安装

    npm install dayjs --save
    

    2、全局配置

    import dayjs from "dayjs"
    Vue.prototype.dayjs = dayjs;  //可以全局使用dayjs
    

    3、局部配置:在子组件引入,将其当成方法使用
    4、返回对象格式

    默认返回一个对象,对象属性如下:
    
    | 描述   | 属性   | 示例值                                           |
    | ---- | ---- | --------------------------------------------- |
    | 日    | $D   | 18                                            |
    | 时    | $H:  | 0                                             |
    | 语言   | $L   | "en"                                          |
    | 月    | $M   | 0                                             |
    | 周    | W    | 1                                             |
    | 日期   | $d   | Mon Jan 18 2021 00:00:00 GMT+0800 (中国标准时间) {} |
    | 秒    | $m:  | 0                                             |
    | 毫秒   | $ms  | 0                                             |
    | 秒    | $s   | 0                                             |
    |      | $x   | {}                                            |
    | 年    | $y   | 2021                                          |
    

    5、格式化日期:format(日期格式)

    dayjs(time).format('YYYY-MM-DD')
    

    6、根据API提供的方法解析:

    dayjs(); //返回一个包含当前日期和时间的 Day.js 对象。
    dayjs('2018-08-08’); // 直接使用的结果
    dayjs(time).format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A’); // 按指定的格式返回的日期
    dayjs(time).set('month', 3).month(); // 通过set 设置月份并获取,dayjs提供了set和get方法
    dayjs(time).add(1, 'year’); // 将年份+1,对年份做增加操作
    dayjs(time).isBefore(dayjs(time)); // 可使用dayjs做两个日期的比较操作
    dayjs('2018-05-05').locale('zh-cn').format(); // locale国际化设置zh-cn,默认为en
    

    7、Day.js 对象是不可变的,所有的 API 操作都将返回一个全新的实例。

    dayjs可接受的数据类型包括:string | number | Date | Dayjs | null | undefined
    dayjs提供的方法:
    clone() 克隆
    isValid() 验证是否是一个有效的时间
    year() 返回年
    month() 返回月
    date() 返回日期
    day() 返回日
    hour() 返回时
    minute() 返回分
    second() 返回秒
    millisecond() 返回毫秒
    set('date', 1) 设置某个类型的值
    get('year') 获取某个类型值
    add() 加法操作
    subtract();  减法
    startOf();  设置到一个时间的开始。
    endOf(); 设置到一个时间的末尾。
    format(); 日期格式化方法
    diff(); 返回指定单位下两个日期时间之间的差异。
    valueOf(); 返回当前实例的 UNIX 时间戳
    unix(); 时间戳
    daysInMonth(); 本月有多少天
    toDate();  返回中国标准时间日期格式
    toJSON();  序列化为 ISO 8601 格式的字符串。
    toISOString(); 返回一个 ISO 8601 格式的字符串。
    toString(); 返回包含时间信息的 string 。
    utcOffset(); 获取 UTC 偏移量 (分钟)。
    isBefore(); 判断是否在另一个提供的日期时间之前
    isSame(); 判断是否和另一个提供的日期时间相同。
    isAfter(); 判断是否在另一个提供的日期时间之后。
    locale(); 国际化
    

    国内文档:https://dayjs.gitee.io/docs/zh-CN/installation/installation

  • 相关阅读:
    css小技巧: select的css控制
    js中不存在exit函数,程序的运行中断停止,可使用return
    转载: WebCore渲染之一:基础
    转载: WEB架构师成长系列索引
    js:<form></form>中有<a>按钮时不能跳转?
    小心得:前台与后台的数据传递
    php session和cookie使用说明
    css 字体使用
    转载: PHP引用(&)使用详解
    三层架构下的增删改查 荣
  • 原文地址:https://www.cnblogs.com/min77/p/15160776.html
Copyright © 2011-2022 走看看