iview admin 將時(shí)間戳 轉(zhuǎn)為 日期格式 (yyyy-MM-dd hh:mm)

時(shí)間戳 js 轉(zhuǎn)換
方法一

//vue  使用
//export function parseTime(time, cFormat) {
function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-u0z1t8os {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    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 time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}


import { parseTime } from '@/utils'  //引用 





var item =[
    {
        "id": 1,
        "timestamp": 743180623584,
        "author": "Eric",
        "reviewer": "Lisa",
        "title": "Rabhtqhxy Qneqbfbzg Aviri Sukgc Lnnoimhwhi Plnssl Fvakje Jsr",
        "content_short": "mock data",
        "forecast": 22.25,
        "importance": 3,
        "type": "JP",
        "status": "published",
        "display_time": "1981-10-22 23:39:35",
        "comment_disabled": true,
        "pageviews": 4999,
        "platforms": [
            "a-platform"
        ]
    },
    {
        "id": 2,
        "timestamp": 1395115030191,
        "author": "Brenda",
        "reviewer": "Matthew",
        "title": "Pghmmrtu Gfiu Ren Sbvadnif Ykrhpb Bqll",
        "content_short": "mock data",
        "forecast": 52.14,
        "importance": 2,
        "type": "CN",
        "status": "draft",
        "display_time": "1970-05-21 15:24:56",
        "comment_disabled": true,
        "pageviews": 684,
        "platforms": [
            "a-platform"
        ]
    },
    {
        "id": 3,
        "timestamp": 575759444799,
        "author": "Barbara",
        "reviewer": "Betty",
        "title": "Rmyuygbdi Ycmo Hyygbddj Cwgtsrcjlw Icsxsdij Wzcld Muru",
        "content_short": "mock data",
        "forecast": 2.73,
        "importance": 2,
        "type": "US",
        "status": "draft",
        "display_time": "1983-01-24 04:55:14",
        "comment_disabled": true,
        "pageviews": 4989,
        "platforms": [
            "a-platform"
        ]
    },
    {
        "id": 4,
        "timestamp": 850499130458,
        "author": "Margaret",
        "reviewer": "Richard",
        "title": "Tcvrhn Mfuczo Vtxusuckc Furbiinw Cypkjvzg",
        "content_short": "mock data",
        "forecast": 79.49,
        "importance": 2,
        "type": "CN",
        "status": "deleted",
        "display_time": "2004-05-27 21:31:56",
        "comment_disabled": true,
        "pageviews": 4390,
        "platforms": [
            "a-platform"
        ]
    } 
]

const filterVal = ['id','timestamp' , 'title', 'author', 'pageviews', 'display_time']
const list = this.list
const data = this.formatJson(filterVal, list)
 function formatJson  (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }

方法二

iview admin 將后臺(tái)時(shí)間戳 轉(zhuǎn)為 日期格式 (yyyy-MM-dd hh:mm),并在組件table表格中使用

1.將源碼全部復(fù)制下來存放在一個(gè)js文件里面(建議在src目錄下新建一個(gè)common文件夾來存放這個(gè)js文件,方便多次復(fù)用)

export function formatDate(date, fmt) {
  let o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小時(shí)
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var 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
}

2.在需要使用的組件引入

import { formatDate } from '@/view/common/date.js'; // 將這里路徑替換成存放js文件的路徑

3.render中函數(shù)使用

{
    title: '日期',
    key: 'title', //注意 key = > params.row.title 對(duì)應(yīng)關(guān)系
    render: (h, params) => {
        return h('div',
            formatDate(new Date(params.row.title), 'yyyy-MM-dd hh:mm')
            // Date是后臺(tái)時(shí)間戳參數(shù)字段
            // 'yyyy-MM-dd hh:mm' 對(duì)應(yīng)的時(shí)間格式 2018-12-21 18:46  
            // 格式可以自行修改,例如 'yyyy-MM-dd' -> 2018-12-21,  'yyyy-MM' -> 2018-12
        )
    }
 }

https://blog.csdn.net/weixin_43233914/article/details/85168854

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容