深圳知名網(wǎng)站廣告發(fā)布平臺(tái)
文章目錄
- 1?? 時(shí)間處理工具
- 1.1 格式化時(shí)間
- 1.2 把時(shí)間戳改成日期格式
- 1.3 Day.js 工具類使用
- 1.4 date-fns 工具類使用
- 優(yōu)質(zhì)資源分享
作者:xcLeigh
文章地址:https://blog.csdn.net/weixin_43151418/article/details/134712978
Electron+Ts+Vue+Vite桌面應(yīng)用系列
:這個(gè)系列包括了從桌面應(yīng)用環(huán)境搭建 到 完整項(xiàng)目運(yùn)行的全過(guò)程。展現(xiàn)一個(gè)完整的窗體應(yīng)用程序,包括對(duì)數(shù)據(jù)的增刪改查,各種表單的應(yīng)用,各種路由的應(yīng)用,登錄注冊(cè)的實(shí)現(xiàn),窗體的放大縮小,列表的應(yīng)用,內(nèi)存的應(yīng)用等。本篇介紹:TypeScript時(shí)間處理工具
1?? 時(shí)間處理工具
1.1 格式化時(shí)間
function formatDate(date:any, fmt:any) {if (!date) {return ''}if (/(y+)/.test(fmt)) {fmt = fmt.replace(RegExp.$1,(date.getFullYear() + '').substr(4 - RegExp.$1.length))}let o:any = {'M+': date.getMonth() + 1,'d+': date.getDate(),'h+': date.getHours(),'m+': date.getMinutes(),'s+': date.getSeconds()}for (let k in o) {if (new RegExp('(' + k + ')').test(fmt))fmt = fmt.replace(RegExp.$1,RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))}return fmt
}
console.log(formatDate(new Date,'yyyy-MM-dd hh:mm:ss'));
//輸出時(shí)間:2023-11-30 15:11:34(當(dāng)然這個(gè)是當(dāng)前時(shí)間,會(huì)一直在變)
1.2 把時(shí)間戳改成日期格式
// 時(shí)間戳(精確到秒,10位)轉(zhuǎn)為'xx年xx月xx日'
public timestampToDate(timestamp: number): string
{ const date = new Date(timestamp*1000);const year = date.getFullYear();const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;const day = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate();const str = `${year}年${month}月${day}日`;return str;
}
1.3 Day.js 工具類使用
Day.js工具
Day.js 是一個(gè)極簡(jiǎn)的 JavaScript 庫(kù),可以為現(xiàn)代瀏覽器解析、驗(yàn)證、操作和顯示日期和時(shí)間。
安裝
npm add dayjs
使用
import dayjs from "dayjs";
//獲取當(dāng)前時(shí)間
console.log("=====>", dayjs().format("YYYY-MM-DD HH:mm:ss"));
//根據(jù)字符串,轉(zhuǎn)時(shí)間對(duì)象
console.log("=====>", dayjs("2024-11-30").format("YYYY-MM-DD HH:mm:ss"));
//根據(jù)時(shí)間戳,轉(zhuǎn)時(shí)間對(duì)象
console.log("=====>", dayjs(1318781876406).format("YYYY-MM-DD HH:mm:ss"));
//格式化時(shí)間對(duì)象
console.log("=====>", dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"));
//獲取年份
console.log("=====>", dayjs().year());
//獲取月份
console.log("=====>", dayjs().month());
//獲取日期
console.log("=====>", dayjs().date());
//傳入對(duì)象
console.log("=====>",dayjs({ year: 2018, month: 8, day: 8 }).format("YYYY-MM-DD HH:mm:ss")
);
//傳入數(shù)組
console.log("=====>", dayjs([2018, 8, 8]).format("YYYY-MM-DD HH:mm:ss"));
//UTC時(shí)間
console.log("=====>", dayjs.utc().format("YYYY-MM-DD HH:mm:ss"));
//獲取當(dāng)前時(shí)間戳
console.log("=====>", dayjs().valueOf());
//復(fù)制對(duì)象
const dayjs1 = dayjs();
const dayjs2 = dayjs1.clone();
console.log("=====>", dayjs1 === dayjs2);
//設(shè)置年份
console.log("=====>", dayjs().set("year", 2018).format("YYYY-MM-DD HH:mm:ss"));
1.4 date-fns 工具類使用
安裝
npm install --save date-fns
相應(yīng)函數(shù)
使用
import { isToday,subDays, format } from 'date-fns';const day = new Date();
console.log(isToday(day)); // 結(jié)果為: true
---參照上面函數(shù)使用
console.log(subDays(-1)); //獲取一天前的日期
優(yōu)質(zhì)資源分享
🧡🧡🧡🧡🤍【總覽】程序員前端、后端資源合集
🧡🧡🧡🧡🤍【源碼】程序員優(yōu)質(zhì)資源匯總
🧡🧡🧡🧡🤍【博主推薦】JAVA SSM框架的后臺(tái)管理系統(tǒng)(附源碼)
🧡🧡🧡🧡🤍【博主推薦】SpringBoot API接口對(duì)數(shù)據(jù)庫(kù)增刪改查,路由,TOKEN,WebSocket完整版(附源碼)
🧡🧡🧡🧡🤍【博主推薦】HTML制作一個(gè)美觀的個(gè)人簡(jiǎn)介網(wǎng)頁(yè)(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html好看的個(gè)人簡(jiǎn)歷網(wǎng)頁(yè)版(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html好看的個(gè)人主頁(yè)(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html好看的邀請(qǐng)函(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html好看的音樂播放器(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html好看的拼圖小游戲(附源碼)
🧡🧡🧡🤍🤍【博主推薦】html好看的拼圖驗(yàn)證碼(附源碼)
🧡🧡🧡🧡🧡【博主推薦】html界面繪制SVG圖形(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html操作SVG圖(附源碼)
🧡🧡🧡🧡🤍【博主推薦】html下拉框樹形(附好看的登錄界面)
🧡🧡🧡🧡🤍【博主推薦】HTML5響應(yīng)式手機(jī)WEB(附源碼)
🧡🧡🧡🧡🤍【博主推薦】大數(shù)據(jù)可視化大屏(源碼下載)
🧡🧡🧡🧡🧡【博主推薦】html引用百度地圖定位閃爍彈框樹形(附源碼)
🧡🧡🧡🧡🤍【博主推薦】HTML酷炫動(dòng)畫表白求愛界面(附源碼)
???? 💞 關(guān)注博主 帶你實(shí)現(xiàn)暢游前后端
???? 🏰 加入社區(qū) 帶你體驗(yàn)馬航不孤單
???? 💯 神秘個(gè)人簡(jiǎn)介 帶你體驗(yàn)不一樣得介紹
???? 🎀 酷炫邀請(qǐng)函 帶你體驗(yàn)高大上得邀請(qǐng)
???? ① 🉑提供云服務(wù)部署(有自己的阿里云);
???? ② 🉑提供前端、后端、應(yīng)用程序、H5、小程序、公眾號(hào)等相關(guān)業(yè)務(wù);
???? 如🈶合作請(qǐng)聯(lián)系我,期待您的聯(lián)系。
????注:本文撰寫于CSDN平臺(tái),作者:xcLeigh(所有權(quán)歸作者所有) ,https://blog.csdn.net/weixin_43151418,如果相關(guān)下載沒有跳轉(zhuǎn),請(qǐng)查看這個(gè)地址,相關(guān)鏈接沒有跳轉(zhuǎn),皆是抄襲本文,轉(zhuǎn)載請(qǐng)備注本文原地址。
???? 親,碼字不易,動(dòng)動(dòng)小手,歡迎 點(diǎn)贊 ? 收藏,如 🈶 問題請(qǐng)留言(評(píng)論),博主看見后一定及時(shí)給您答復(fù),💌💌💌
原文地址:https://blog.csdn.net/weixin_43151418/article/details/134712978(防止抄襲,原文地址不可刪除)