dayjs 使用笔记
生活随笔
收集整理的這篇文章主要介紹了
dayjs 使用笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
全局配置 local 和 插件,通常在你的入口函數中調用
import dayjs from "dayjs";
import weekday from "dayjs/plugin/weekday";
import "dayjs/locale/zh-cn";
// config dayjs
dayjs.extend(weekday);
dayjs.locale('zh-cn')
這周
// 設置了zh-cn 一周的開始指向了星期一,而不是星期日
// startOf('day') 將時間指向了 00:00
const start = dayjs().weekday(0).startOf("day");
// dayjs 默認生成現在的時間
const end = dayjs();
// 將dayjs對象格式化為字符串
const range = [start.format(), end.format()];
這個月
// 這個月一號0時0分
const start = dayjs().startOf("M");
const end = dayjs();
const range = [start.format(), end.format()];
最近三個月
// 從當前時間減去2個月,然后獲取那個月的第一天0時0分
const start = dayjs().subtract(2, "M") .startOf("M");
const end = dayjs();
增加時間
dayjs('2000-1-1 11:00:00') .add(2, 'h') .format("YYYY-MM-DD hh:mm:ss") // 2000-01-01 01:00:00
dayjs('2000-1-1 11:00:00') .add(2, 'h') .format("YYYY-MM-DD HH:mm:ss") // 2000-01-01 13:00:00
時間比較
const date1 = dayjs();
const date2 = dayjs().add(1, 'h');
// date1 在 date2 之前 (date1 < date2)
date1.isBefore( date2 ) // true
See alse:
dayjs官網
總結
以上是生活随笔為你收集整理的dayjs 使用笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【编译原理】FIRSTVT和LASTVT
- 下一篇: ✳编程求当n≤100时,n!的准确值