vue中對時間戳的處理方式
更新時間:2022年06月02日 11:39:08 作者:Gorgio_Liu
這篇文章主要介紹了vue中對時間戳的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
vue對時間戳的處理
1、自建js文件
文件位置根據(jù)自己的項目位置自定義
export function formatDate(date, fmt) {
? ? if (/(y+)/.test(fmt)) {
? ? ? ? fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
? ? }
? ? let o = {
? ? ? ? '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)) {
? ? ? ? ? ? let str = o[k] + '';
? ? ? ? ? ? fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
? ? ? ? }
? ? }
? ? return fmt;
};
function padLeftZero(str) {
? ? return ('00' + str).substr(str.length);
};2、在組件模板中引用定義好的時間戳函數(shù)
<template>
? ? <div>{{time | formatDate}}</div>
</template>3、設置好在script中的引用和定義
<script>
import {formatDate} from 'xxx.js';
export default {
? ? filters: {
? ? ? ? formatDate(time) {
? ? ? ? ? ? var date = new Date(time);
? ? ? ? ? ? return formatDate(date, 'yyyy-MM-dd hh:mm');
? ? ? ? }
? ? }
}
</script>vue時間戳轉換
后臺拿時間數(shù)據(jù),一般不是時間格式,而是一串數(shù)組,這個時候就需要轉換,然后才可以使用了
?add_time(row, column, cellValue, index) {
? ? ? ? if (cellValue == null || cellValue == "") return "";
? ? ? ? let date = new Date(parseInt(cellValue) * 1000);
? ? ? ? let Y = date.getFullYear() + '-';
? ? ? ? let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
? ? ? ? let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
? ? ? ? let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
? ? ? ? let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
? ? ? ? let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
? ? ? ? return Y + M + D;
? ? ? },以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue動態(tài)的 BreadCrumb 組件el-breadcrumb ElementUI詳解
這篇文章主要介紹了vue如何做一個動態(tài)的 BreadCrumb 組件,el-breadcrumb ElementUI2024-07-07
,本文通過圖文示例代碼相結合給大家介紹的非常詳細,需要的朋友可以參考下
vue生成二維碼QR?Code的簡單實現(xiàn)方法示例
這篇文章主要為大家介紹了vue生成二維碼QR?Code的實現(xiàn)示例詳情,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04

