將時間戳轉化成「時間:分鐘」的形式:less
time(first) { const date = new Date(first * 1000); // this.commentTime = date.toLocaleString(); const hour = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours(); const minute = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes(); const dateTime = `${hour}:${minute}`; return dateTime;},
這邊toLocaleString的話也行,但須要轉化時區。仍是本身封裝一個吧。ide
使用的時候:post
<span class="message-time" :value="time(comInfo.create_time)" ></span>
2020.08.23更新ui
/** * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat * @returns {string | null} */export function parseTime(time, cFormat) { if (arguments.length === 0) {return null; } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'; let date; if (typeof time === 'object') {date = time; } else {if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time, 10);}if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000;}date = new Date(time); } const formatObj = {y: date.getFullYear(),m: date.getMonth() + 1,d: date.getDate(),h: date.getHours(),i: date.getMinutes(),s: date.getSeconds(),a: date.getDay(), }; const timeStr = format.replace(/{([ymdhisa])+}/g, (result, key) => {const value = formatObj[key];// Note: getDay() returns 0 on Sundayif (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value];}return value.toString().padStart(2, '0'); }); return timeStr;}/** * @param {number} time * @param {string} option * @returns {string} */export function formatTime(time, option) { if ((`${time}`).length === 10) {time = parseInt(time, 10) * 1000; } else {time = +time; } const d = new Date(time); const now = Date.now(); const diff = (now - d) / 1000; if (diff < 30) {return '剛剛'; } if (diff < 3600) {// less 1 hourreturn `${Math.ceil(diff / 60)}分鐘前`; } if (diff < 3600 * 24) {return `${Math.ceil(diff / 3600)}小時前`; } if (diff < 3600 * 24 * 2) {return '1天前'; } if (option) {return parseTime(time, option); } return (`${d.getMonth() + 1}月${ d.getDate()}日`// ${// d.getHours()// }時${// d.getMinutes()// }分` );}export function param2Obj(url) { const search = url.split('?')[1]; if (!search) {return {}; } return JSON.parse(`{"${decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') .replace(/\+/g, ' ') }"}`);}
使用:this
<viui-text style="margin-left: 90rt;font-size: 12dp;lineHeight: 24dp;" :style="messageTime" > {{ comInfo.create_time | formatTime }} </viui-text>import { formatTime } from '@util/formatDate'; export default { name: 'XXX', filters: { formatTime, }, }
還有一種:url
/** * 發表時間轉換 (多久之前發表的) * @export * @param {String} seconds 發表時間距離當前時間的秒數 * @returns {String}} 轉換後的文字描述 * example1:65 => 1分鐘前 * example2:3622 => 1小時前 */export function transTimePassed(postTime) { // seconds非0,且等於''或null或undefine時,則不顯示 if (!postTime) {return ''; } const now = (new Date().getTime()) / 1000; const seconds = now - (Number(postTime)); if (seconds <= 0) {return '剛剛發佈'; } if (seconds < 60) {return `${Math.floor(seconds)}秒前`; } const minutes = seconds / 60; if (minutes < 60) {return `${Math.floor(minutes)}分鐘前`; }; const hours = minutes / 60; if (hours < 24) {return `${Math.floor(hours)}小時前`; }; const days = hours / 24; if (days <= 7) {return `${Math.floor(days)}天前`; }; // const months = days / 30; if (days >= 7) {const jsTimestamp = new Date(postTime * 1000);const year = jsTimestamp.getFullYear();const hour = jsTimestamp.getHours();const minute = jsTimestamp.getMinutes();const seconds = jsTimestamp.getSeconds();const currentYear = new Date().getFullYear();if (currentYear === year) { return `${jsTimestamp.getMonth() + 1}月${jsTimestamp.getDate()}日${hour}時${minute}分${seconds}秒`;}return `${year}年${jsTimestamp.getMonth() + 1}月${jsTimestamp.getDate()}日${hour}時${minute}分${seconds}秒`; } return '';}
爲啥越看之前的代碼越有一種本身怎麼這麼笨的感受,噗吐血了…spa