日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

自定义new Date()格式显示,适用JavaScript / Nodejs / Vue / React / UniApp / 其他基于js工程的项目

發(fā)布時(shí)間:2023/12/29 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义new Date()格式显示,适用JavaScript / Nodejs / Vue / React / UniApp / 其他基于js工程的项目 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

定義一個(gè)文件去存儲(chǔ)下列代碼,全局引入就可以使用了

// 直接new就可以了 let time = new Date().Format();

下面直接上代碼:

/*create By Jane 時(shí)間格式化 yyyy-MM-dd hh:mm:ss**/ Date.prototype.Format = function(fmt = 'yyyy-MM-dd hh:mm:ss') {const o = {'M+': this.getMonth() + 1, //月份'd+': this.getDate(), //日'h+': this.getHours(), //小時(shí)'m+': this.getMinutes(), //分's+': this.getSeconds(), //秒'q+': Math.floor((this.getMonth() + 3) / 3), //季度S: this.getMilliseconds(), //毫秒};if (/(y+)/.test(fmt)) {fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));}for (const 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; }; Date.prototype.FormatDateTime = function() {return this.Format('yyyy-MM-dd hh:mm:ss'); }; Date.prototype.FormatDate = function() {return this.Format('yyyy-MM-dd'); }; Date.prototype.FormatTime = function() {return this.Format('hh:mm:ss'); };//時(shí)間簡(jiǎn)化 轉(zhuǎn)換為 剛剛 今天 昨天 之類 Date.prototype.Short = function() {const dateTimeStamp = this.getTime();const minute = 1000 * 60; //把分,時(shí),天,周,半個(gè)月,一個(gè)月用毫秒表示const hour = minute * 60;const day = hour * 24;const week = day * 7;const month = day * 30;const now = new Date().getTime(); //獲取當(dāng)前時(shí)間毫秒const diffValue = now - dateTimeStamp;//時(shí)間差if (diffValue < 0) {return;}const minC = parseInt(diffValue / minute); //計(jì)算時(shí)間差的分,時(shí),天,周,月const hourC = parseInt(diffValue / hour);const dayC = parseInt(diffValue / day);const weekC = parseInt(diffValue / week);const monthC = parseInt(diffValue / month);let result;if (monthC >= 1 && monthC <= 3) {result = ' ' + monthC + '月前';} else if (weekC >= 1 && weekC <= 3) {result = ' ' + weekC + '周前';} else if (dayC >= 1 && dayC <= 6) {result = ' ' + dayC + '天前';} else if (hourC >= 1 && hourC <= 23) {result = ' ' + hourC + '小時(shí)前';} else if (minC >= 1 && minC <= 59) {result = ' ' + minC + '分鐘前';} else if (diffValue >= 0 && diffValue <= minute) {result = '剛剛';} else {const datetime = new Date();datetime.setTime(dateTimeStamp);const Nyear = datetime.getFullYear();const Nmonth = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;const Ndate = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate();// var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();// var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();// var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();result = Nyear + '-' + Nmonth + '-' + Ndate;}return result; };

總結(jié)

以上是生活随笔為你收集整理的自定义new Date()格式显示,适用JavaScript / Nodejs / Vue / React / UniApp / 其他基于js工程的项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。