Vue.js 時(shí)間轉(zhuǎn)換代碼及時(shí)間戳轉(zhuǎn)時(shí)間字符串
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; } //使用方法 var now = new Date(); var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); //使用方法2: var testDate = new Date(); var testStr = testDate.format("YYYY年MM月dd日hh小時(shí)mm分ss秒"); alert(testStr); //示例: alert(new Date().format("yyyy年MM月dd日")); alert(new Date().format("MM/dd/yyyy")); alert(new Date().format("yyyyMMdd")); alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
代碼:
// 格式化formatter中顯示的時(shí)間格式 // Date.prototype.Format = function(fmt) { // 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; // };
應(yīng)用:
1、 2018-10-10 轉(zhuǎn) 年月日
// new Date(this.envPlanList[i].starttime.slice(0, 4), // this.envPlanList[i].starttime.slice(5, 7), // this.envPlanList[i].starttime.slice(8, 10)), // new Date(this.envPlanList[i].endtime.slice(0, 4), // this.envPlanList[i].endtime.slice(5, 7), // this.envPlanList[i].endtime.slice(8, 10)),
2、 年月日 轉(zhuǎn) 2018-10-10
formatter(params) { return `${params.name}: ${new Date(params.value[1]).Format('yyyy/MM/dd')} - - ${new Date(params.value[2]).Format('yyyy/MM/dd')} -- ${params.value[3]}`; // return `${params.name}: ${params.value[1]} -- ${params.value[2]} -- ${params.value[3]}`; },
下面看下vue.js時(shí)間戳轉(zhuǎn)時(shí)間字符串
formartDate(param) { let date = new Date(param); Y = date.getFullYear() + '-'; M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-'; D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '; h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'; m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':'; s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); return Y + M + D + h + m + s; }
總結(jié)
以上所述是小編給大家介紹的Vue.js 時(shí)間轉(zhuǎn)換代碼及時(shí)間戳轉(zhuǎn)時(shí)間字符串,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JavaScript中各種時(shí)間轉(zhuǎn)換問(wèn)題詳解(YYYY-MM-DD、時(shí)間戳、中國(guó)標(biāo)準(zhǔn)時(shí)間)
- 關(guān)于導(dǎo)入excel時(shí)js轉(zhuǎn)換時(shí)間的正確方式
- JS的時(shí)間格式化和時(shí)間戳轉(zhuǎn)換函數(shù)示例詳解
- js時(shí)間轉(zhuǎn)換毫秒的實(shí)例代碼
- JS將時(shí)間秒轉(zhuǎn)換成天小時(shí)分鐘秒的字符串
- 淺談在Vue.js中如何實(shí)現(xiàn)時(shí)間轉(zhuǎn)換指令
- js實(shí)現(xiàn)把時(shí)間戳轉(zhuǎn)換為yyyy-MM-dd hh:mm 格式(es6語(yǔ)法)
- JS中國(guó)標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)化為年月日時(shí)分秒'yyyy-MM-dd hh:mm:ss'的示例詳解
相關(guān)文章
Vue3中reactive函數(shù)toRef函數(shù)ref函數(shù)簡(jiǎn)介
這篇文章主要介紹了Vue3中的三種函數(shù),分別對(duì)reactive函數(shù)toRef函數(shù)以及ref函數(shù)原理及使用作了簡(jiǎn)單介紹,有需要的朋友可以借鑒參考下2021-09-09Vue?socket.io模塊實(shí)現(xiàn)聊天室流程詳解
vue-socket.io其實(shí)是在socket.io-client(在瀏覽器和服務(wù)器之間實(shí)現(xiàn)實(shí)時(shí)、雙向和基于事件的通信)基礎(chǔ)上做了一層封裝,將socket掛載到vue實(shí)例上,同時(shí)可使用sockets對(duì)象輕松實(shí)現(xiàn)組件化的事件監(jiān)聽,在vue項(xiàng)目中使用起來(lái)更方便2022-12-12vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作
這篇文章主要介紹了vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Vue+axios實(shí)現(xiàn)統(tǒng)一接口管理的方法
這篇文章主要介紹了Vue+axios實(shí)現(xiàn)統(tǒng)一接口管理的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Vite內(nèi)網(wǎng)ip訪問(wèn)2種配置方式
這篇文章主要給大家介紹了關(guān)于Vite內(nèi)網(wǎng)ip訪問(wèn)的2種配置方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vite具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07Vue+Typescript中在Vue上掛載axios使用時(shí)報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了Vue+Typescript中在Vue上掛載axios使用時(shí)報(bào)錯(cuò)問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-08-08Vue3?使用Element?Plus表格單選帶checkbox功能
這篇文章主要介紹了Vue3?使用Element?Plus表格單選帶checkbox,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11