當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
自定义new Date()格式显示,适用JavaScript / Nodejs / Vue / React / UniApp / 其他基于js工程的项目
生活随笔
收集整理的這篇文章主要介紹了
自定义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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电力工程师 计算机英语,电力工程师专业英
- 下一篇: JavaScript—基础Day2