溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Vue.js中將時間轉(zhuǎn)換成時間戳

發(fā)布時間:2021-02-25 16:00:54 來源:億速云 閱讀:1730 作者:戴恩恩 欄目:web開發(fā)

這篇文章主要介紹了如何在Vue.js中將時間轉(zhuǎn)換成時間戳,此處通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考價值,需要的朋友可以參考下:

為什么要使用Vue

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護性和可測試性更強的代碼庫,Vue允許可以將一個網(wǎng)頁分割成可復(fù)用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎng)頁中相應(yīng)的地方,所以越來越多的前端開發(fā)者使用vue。

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小時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中顯示的時間格式
// Date.prototype.Format = function(fmt) {
 // const o = {
// 'M+': this.getMonth() + 1, // 月份
// 'd+': this.getDate(), // 日
// 'h+': this.getHours(), // 小時
// '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時間戳轉(zhuǎn)時間字符串

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;
}

到此這篇關(guān)于如何在Vue.js中將時間轉(zhuǎn)換成時間戳的文章就介紹到這了,更多相關(guān)如何在Vue.js中將時間轉(zhuǎn)換成時間戳的內(nèi)容請搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI