Dayjs

安裝

dayjs官網
所有程式碼都應該在這兩種環境中運行,並且所有單元測試都在這兩種環境中運行。

目前,ci 系統使用以下瀏覽器:Windows XP 上的 Chrome、Windows 7 上的 IE 8、9 和 10、Windows 10 上的 IE 11、Linux 上最新的 Firefox 以及 OSX 10.8 和 10.11 上最新的 Safari。

瀏覽器使用

cdn

1
2
3
<!-- CDN example (jsDelivr) -->
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script>dayjs().format()</script>

typescript

參閱官網Typescript安裝

1
2
3
npm install --save dayjs
import * as dayjs from 'dayjs'
dayjs().format()

區域設定和插件導入

要使用語言環境和插件,您首先需要匯入目標語言和插件。

1
2
3
4
5
6
import * as dayjs from 'dayjs'
import * as isLeapYear from 'dayjs/plugin/isLeapYear' // import plugin
import 'dayjs/locale/zh-cn' // import locale

dayjs.extend(isLeapYear) // use plugin
dayjs.locale('zh-cn') // use locale

處理打包dayjs錯誤訊息

打包dayjs錯誤訊息

1
2
3
4
5
6
{ //tsconfig.json
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
}
}

npm

1
2
3
4
5
npm install --save dayjs
# or
yarn add --save dayjs
# or
pnpm add --save dayjs

npm 引入或使用

1
2
3
4
5
// 引入 Day.js
const dayjs = require('dayjs')

// 或使用 ES6 import 方式引入 Day.js,(Angular ,Vue,React 使用頁面引入方法)
import dayjs from 'dayjs'

使用方式

dayjs取得目前時間為字符

1
2
const now = new Date();//等同 dayjs(new Date());"2024-04-20T05:25:08.194Z"
const now = dayjs() // 等同 dayjs(new Date());"2024-04-20T05:25:08.194Z"

可傳入 Data 物件取得目前時間

1
const now = dayjs(new Date());"2024-04-20T05:25:08.194Z";

*從字符”2024-04-20T05:25:08.194Z”取得時間搓

1
2
const timestamp = new Date().getTime();
console.log('timestamp',timestamp);//1713590708194

取得特定時間的年月日時等資訊

1
2
3
4
5
6
7
8
console.log('取得現在年份', dayjs().year())
console.log('取得現在月份', dayjs().month() + 1)
console.log('取得現在日', dayjs().date())
console.log('取得星期', dayjs().day())
console.log('取得時', dayjs().hour())
console.log('取得分', dayjs().minute())
console.log('取得秒', dayjs().second())
console.log('取得豪秒',dayjs().millisecond())

取得 UTC 時間

1
2
3
4
5
6
7
dayjs.extend(utc)

// 取得本地時間
dayjs().format() //2019-03-06T08:00:00+08:00

// 取得 UTC 時間
dayjs.utc().format() // 2019-03-06T00:00:00Z

驗證返回的時間是否正確

1
2
dayjs(null).isValid() // false
dayjs().isValid() // true

取得

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 取得毫秒
dayjs().millisecond()
// 設定毫秒,如傳入數字在 0-999 範圍外,會進位到秒。
dayjs().millisecond(100)

// 取得秒
dayjs().seconds()
// 設定秒,如傳入數字在 0-59 範圍外,會進位到分鐘。
dayjs().seconds(30)


// 取得分鐘
dayjs().minute()
// 設定秒,如傳入數字在 0-59 範圍外,會進位到小時。
dayjs().minute(30)

// 取得小時
dayjs().hour()
// 設定小時,如傳入數字在 0-23 範圍外,會進位到天數。
dayjs().hour(12)

// 取得天數
dayjs().date()
// 設定天數,如傳入數字在 0-月份天數 範圍外,會進位到月份。
dayjs().date(1)

// 取得星期(幾)
dayjs().day()
// 設定星期(幾),如傳入數字在 0-6 範圍外,會進位到週。
dayjs().day(0)

// 取得月份
dayjs().month()
// 設定月份,如傳入數字在 0-11 範圍外,會進位到年。
dayjs().month(6)

// 取得年份
dayjs().year()
// 設定年份
dayjs().year(2020)

格式化時間

1
2
3
4
5
6
7
8
9
10
dayjs().format("YYYY-MM-DD HH:mm:ss"); // 2025-03-25 21:11:06

// 默認返回 ISO 8601 時間字串,例:'2020-05-15T10:59:34+08:00'
dayjs().format()

// 返回 'YYYYescape 2019-01-25T00:00:00-02:00Z'
dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]')

// 返回 '25/01/2019'
dayjs('2019-01-25').format('DD/MM/YYYY')

加減時間

1
2
3
4
5
// 取得現在日加1日
dayjs().add(1, "day").date();

// 取得現在日減1日
dayjs().subtract(1, "day").date();

計算時間差

1
2
3
4
// 計算兩個時間的時間差(天)
dayjs().diff(dayjs("2025-03-24"), "day"); //-3
// 計算兩個時間的時間差分鐘
dayjs().diff(dayjs("2025-03-20 11:00"), "minute"); //-43

起始時間 / 結束時間

1
2
dayjs().startOf("day").format('YYYY/MM/DD')
dayjs().endOf("day").format('YYYY/MM/DD')

轉換成 unix 時間戳記

使用 valueOf() 可以將時間轉換成 unix 時間戳記

1
2
3
4
5
6
7
dayjs().valueOf() // 1742437455396

// 情境:時間戳 => YYYY-MM-DD HH:mm:s
dayjs(item.buildDate).format('YYYY-MM-DD HH:mm:ss')

//情境:YYYY-MM-DD HH:mm:s => 時間戳
dayjs(item.buildDate).valueOf()