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().date(1).startOf("M");

const end = dayjs();
const range = [start.format(), end.format()];

最近三個月

// 從當前時間減去2個月,然後獲取那個月的第一天0時0分
const start = dayjs().subtract(2, "M") .date(1) .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:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章