一文詳細(xì)講下day.js的基礎(chǔ)用法(干貨滿滿)
Day.js 是一個(gè)功能強(qiáng)大且易于使用的日期處理庫(kù),適合在項(xiàng)目中進(jìn)行日期和時(shí)間的操作。通過(guò)插件可以擴(kuò)展更多功能,滿足不同的需求。
1.安裝
npm install dayjs
2. 基礎(chǔ)日期解析和格式化
import dayjs from 'dayjs' // 1. 創(chuàng)建日期對(duì)象 const now = dayjs() // 當(dāng)前時(shí)間 const date1 = dayjs('2024-03-14') // 從字符串創(chuàng)建 const date2 = dayjs('2024-03-14 10:30:00') // 帶時(shí)間的字符串 const date3 = dayjs(new Date()) // 從 Date 對(duì)象創(chuàng)建 const date4 = dayjs(1710400000000) // 從時(shí)間戳創(chuàng)建 // 2. 常用格式化 console.log(now.format('YYYY-MM-DD')) // 2024-03-14 console.log(now.format('YYYY年MM月DD日')) // 2024年03月14日 console.log(now.format('YYYY-MM-DD HH:mm:ss')) // 2024-03-14 10:30:00 console.log(now.format('dddd')) // 星期四 console.log(now.format('MM/DD/YYYY')) // 03/14/2024 console.log(now.format('HH:mm')) // 10:30
3.日期獲取和修改
// 1. 獲取日期部分 const date = dayjs('2024-03-14 10:30:00') console.log(date.year()) // 2024 console.log(date.month()) // 2 (0-11) console.log(date.date()) // 14 console.log(date.day()) // 4 (0-6, 0是星期天) console.log(date.hour()) // 10 console.log(date.minute()) // 30 console.log(date.second()) // 0 // 2. 修改日期 const newDate = date .year(2025) // 設(shè)置年份 .month(3) // 設(shè)置月份 .date(15) // 設(shè)置日期 .hour(14) // 設(shè)置小時(shí) .minute(45) // 設(shè)置分鐘 .second(30) // 設(shè)置秒數(shù) console.log(newDate.format('YYYY-MM-DD HH:mm:ss'))
4.日期操作和計(jì)算
const date = dayjs('2024-03-14') // 1. 增加時(shí)間 console.log(date.add(1, 'day').format('YYYY-MM-DD')) // 增加1天 console.log(date.add(1, 'week').format('YYYY-MM-DD')) // 增加1周 console.log(date.add(1, 'month').format('YYYY-MM-DD')) // 增加1月 console.log(date.add(1, 'year').format('YYYY-MM-DD')) // 增加1年 console.log(date.add(2, 'hours').format('HH:mm')) // 增加2小時(shí) // 2. 減少時(shí)間 console.log(date.subtract(1, 'day').format('YYYY-MM-DD')) // 減少1天 console.log(date.subtract(1, 'month').format('YYYY-MM-DD')) // 減少1月 // 3. 開(kāi)始和結(jié)束時(shí)間 console.log(date.startOf('year').format('YYYY-MM-DD')) // 年初 console.log(date.startOf('month').format('YYYY-MM-DD')) // 月初 console.log(date.startOf('week').format('YYYY-MM-DD')) // 周初 console.log(date.startOf('day').format('HH:mm:ss')) // 天初 console.log(date.endOf('year').format('YYYY-MM-DD')) // 年末 console.log(date.endOf('month').format('YYYY-MM-DD')) // 月末
5.日期比較
const date1 = dayjs('2024-03-14') const date2 = dayjs('2024-03-15') // 1. 基礎(chǔ)比較 console.log(date1.isBefore(date2)) // true console.log(date1.isAfter(date2)) // false console.log(date1.isSame(date2)) // false // 2. 具體單位的比較 console.log(date1.isSame(date2, 'month')) // true (同月) console.log(date1.isSame(date2, 'year')) // true (同年) // 3. 比較大小 console.log(date1.diff(date2)) // 時(shí)間差(毫秒) console.log(date1.diff(date2, 'day')) // 時(shí)間差(天) console.log(date1.diff(date2, 'month')) // 時(shí)間差(月)
6.實(shí)用插件功能
// 1. 相對(duì)時(shí)間插件 import relativeTime from 'dayjs/plugin/relativeTime' import 'dayjs/locale/zh-cn' dayjs.extend(relativeTime) dayjs.locale('zh-cn') console.log(dayjs().fromNow()) // 幾秒前 console.log(dayjs().from(dayjs('2024-01-01'))) // 2個(gè)月前 // 2. 是否相同或之前/之后插件 import isSameOrBefore from 'dayjs/plugin/isSameOrBefore' import isSameOrAfter from 'dayjs/plugin/isSameOrAfter' dayjs.extend(isSameOrBefore) dayjs.extend(isSameOrAfter) console.log(date1.isSameOrBefore(date2)) // true console.log(date1.isSameOrAfter(date2)) // false // 3. 周數(shù)插件 import weekOfYear from 'dayjs/plugin/weekOfYear' dayjs.extend(weekOfYear) console.log(dayjs().week()) // 獲取當(dāng)前是第幾周
7.實(shí)際應(yīng)用示例
// 1. 格式化日期顯示 const formatDate = (date) => { return dayjs(date).format('YYYY-MM-DD HH:mm:ss') } // 2. 計(jì)算剩余時(shí)間 const getRemainingTime = (endDate) => { const end = dayjs(endDate) const now = dayjs() const days = end.diff(now, 'day') const hours = end.diff(now, 'hour') % 24 const minutes = end.diff(now, 'minute') % 60 return `${days}天${hours}小時(shí)${minutes}分鐘` } // 3. 獲取日期范圍 const getDateRange = (start, end) => { const dates = [] let current = dayjs(start) while (current.isBefore(end) || current.isSame(end)) { dates.push(current.format('YYYY-MM-DD')) current = current.add(1, 'day') } return dates } // 4. 判斷是否是工作日 const isWorkday = (date) => { const day = dayjs(date).day() return day !== 0 && day !== 6 } // 5. 獲取月份天數(shù) const getDaysInMonth = (date) => { return dayjs(date).daysInMonth() }
這些是 Day.js 最常用和最實(shí)用的功能。Day.js 的 API 設(shè)計(jì)簡(jiǎn)單直觀,鏈?zhǔn)秸{(diào)用使得代碼更加清晰。根據(jù)實(shí)際項(xiàng)目需求,你可以選擇性地使用這些功能。
總結(jié)
到此這篇關(guān)于day.js基礎(chǔ)用法的文章就介紹到這了,更多相關(guān)day.js基礎(chǔ)用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript中的location、history、navigator對(duì)象實(shí)例介紹
這篇文章主要介紹了JavaScript中的location、history、navigator對(duì)象實(shí)例介紹,需要的朋友可以參考下2023-05-05移動(dòng)端圖片上傳旋轉(zhuǎn)、壓縮問(wèn)題的方法
在本篇文章中我們給大家分享了關(guān)于移動(dòng)端圖片上傳旋轉(zhuǎn)、壓縮問(wèn)題的解決方法,有需要的朋友們參考下2018-10-10JavaScript實(shí)現(xiàn)一個(gè)空中避難的小游戲
最近利用Javascript實(shí)現(xiàn)了一個(gè)小游戲,覺(jué)著還不錯(cuò),所以分享給大家,下面這篇文章主要給大家介紹了利用JavaScript實(shí)現(xiàn)一個(gè)空中避難的小游戲的相關(guān)資料,文中給出了完整的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-06-06JavaScript解八皇后問(wèn)題的方法總結(jié)
在國(guó)際象棋的8*8棋盤(pán)上如何擺放8個(gè)皇后使任一皇后無(wú)法吃掉其他皇后的問(wèn)題便是最初的八皇后問(wèn)題,此后也被不斷擴(kuò)展而作為經(jīng)典的算法題目,這里我們就來(lái)看一下JavaScript解八皇后問(wèn)題的方法總結(jié)2016-06-06javascript顯示倒計(jì)時(shí)控制按鈕的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇javascript顯示倒計(jì)時(shí)控制按鈕的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06JavaScript設(shè)置名字輸入不合法的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript設(shè)置名字輸入不合法的方法,需要的朋友可以參考下2017-05-05一個(gè)JavaScript防止表單重復(fù)提交的實(shí)例
防止重復(fù)表單提交的方法有很多,本文使用JavaScript來(lái)實(shí)現(xiàn)防止表單重復(fù)提交,很簡(jiǎn)單,但很實(shí)用,新手朋友們不要錯(cuò)過(guò)2014-10-10