時間處理工具day.js常用方法
一、時間格式
Tue Mar 28 2023 17:26:39 GMT+0800 //(中國標準時間) 2021-07-29T21:35:54+08:00 //末尾存在+,代表時間格式為包括時區(qū)的時間格式,+08:00代表東八區(qū) 2021-07-29T21:35:54Z //末尾有Z的,為ISO格式的時間,代表UTC時間(UTC:世界標準時間,即格林威治標準時間,初中學的本初子午線),不帶時區(qū),假如中國地區(qū)(東八區(qū))的去看這個時間要再加8h
二、dayjs()格式化
dayjs()等價于dayjs(Date.now())、dayjs(new Date())
獲取到的時間格式為:Tue Mar 28 2023 17:26:39 GMT+0800 (中國標準時間)
dayjs()對象格式化:
格式化dayjs()對象為YYYY-MM-DD HH:mm:ss格式
dayjs (時間).format('YYYY-MM-DD HH:mm:ss')
三、dayjs()獲取年月日時分秒
console.log("dayjs().get('year'):", dayjs().get("year")); //年 [1,366] console.log("dayjs().get('month'):", dayjs().get("month")); //月 [0,11] 0表示1月 console.log("dayjs().get('date'):", dayjs().get("date")); //日[1,31] console.log("dayjs().get('hour'):", dayjs().get("hour")); //時 [0,23] console.log("dayjs().get('minute'):", dayjs().get("minute")); //分 [0,59] console.log("dayjs().get('second'):", dayjs().get("second")); //秒 [0,59] console.log("dayjs().get('millisecond'):", dayjs().get("millisecond")); //毫秒[0,999] console.log("dayjs().get('day'):", dayjs().get("day")); //星期幾 [0,6]。0(星期日)到6(星期六)
四、dayjs()計算
加減指定時間
dayjs().add(3,”year”) dayjs().subtract(5,”minute”)
計算差值
let time1 = "2023-03-28 14:28:04" let time2 = "2022-04-15 12:05:58" Time2.diff(time,”hour”) //相差多少小時 Time2.diff(time1,”minute”) //相加多少分鐘
五、dayjs()判斷
判斷大小
console.log("當前時間:",dayjs().format("YYYY-MM-DD")) console.log("當前時間< 2022-01-01 嗎):",dayjs().isBefore(dayjs('2022-01-01'))) console.log("當前時間 > 2022-01-01 嗎):",dayjs().isAfter(dayjs('2022-01-01'))) console.log("當前時間 = 222-01-01 嗎):",dayjs().isSame(dayjs('2022-01-01')))
判斷是否在兩數(shù)之間
import dayjs from "dayjs" import isBetween from "dayjs/plugin/isBetween" dayjs.extend(isBetween);
六、安裝
安裝:npm i -S dayjs
局部引入:import dayjs from "dayjs";
報錯:TypeError: _ctx.dayjs is not a function
app.config.globalProperties.$dayjs = dayjs // 全局引入,原型掛載
附:使用dayjs獲取當前時間
1、在項目中安裝dasjs: npm install --save dasjs
2、vue中局部引用: import dayjs from 'dayjs';
3、實時拿到當前時間,代碼如下:
<template> <div class="bar-time"> <div>{{ timeStr.time }}</div> <div>{{ timeStr.year }}</div> </div> </template> <script lang="ts" setup> import { ref, reactive } from 'vue'; import dayjs from 'dayjs'; /** * 導航欄時間設置 * time 時間 * year 年月日 */ const timeStr = reactive({ time: ``, year: ``, }); // 設置定時器實時拿到當前時間 const getNowTime = () => { setInterval(() => { timeStr.time = dayjs(`${new Date()}`).format('HH:mm:ss'); timeStr.year = dayjs(`${new Date()}`).format('YYYY年MM月DD日'); }, 1000); }; getNowTime(); </script>
總結(jié)
到此這篇關(guān)于時間處理工具day.js常用方法的文章就介紹到這了,更多相關(guān)day.js常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實現(xiàn)定時自動關(guān)閉DIV層提示框的方法
這篇文章主要介紹了JS實現(xiàn)定時自動關(guān)閉DIV層提示框的方法,可實現(xiàn)加載時載入js代碼控制div層提示框自動關(guān)閉的效果,非常簡單實用,需要的朋友可以參考下2015-05-05setTimeout內(nèi)不支持jquery的選擇器的解決方案
在JS中無論是setTimeout還是setInterval,在使用函數(shù)名作為調(diào)用句柄時都不能帶參數(shù),而在許多場合必須要帶參數(shù),這就需要想方法解決。2015-04-04利用IntersectionObserver實現(xiàn)動態(tài)渲染的示例詳解
IntersectionObserver誕生已經(jīng)有幾年了,所以它的兼容性目前已經(jīng)達到可以使用的程度了。本文主要介紹了如何利用IntersectionObserver實現(xiàn)動態(tài)渲染,感興趣的可以了解一下2022-12-12前端url拼接參數(shù)格式&?用&和??=拼接方法實例
在一些情況下需要直接往url上拼接請求參數(shù),下面這篇文章主要給大家介紹了關(guān)于前端url拼接參數(shù)格式&?用&和??=拼接的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-02-02JS對象與json字符串相互轉(zhuǎn)換實現(xiàn)方法示例
這篇文章主要介紹了JS對象與json字符串相互轉(zhuǎn)換實現(xiàn)方法,結(jié)合實例形式分析了js對象與json字符串相互轉(zhuǎn)換的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-06-06