时间戳 js 转换
方法一
//vue 使用
//export function parseTime(time, cFormat) {
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)
}
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 将后台时间戳 转为 日期格式 (yyyy-MM-dd hh:mm),并在组件table表格中使用
1.将源码全部复制下来存放在一个js文件里面(建议在src目录下新建一个common文件夹来存放这个js文件,方便多次复用)
export function formatDate(date, fmt) {
let o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'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中函数使用
{
title: '日期',
key: 'title', //注意 key = > params.row.title 对应关系
render: (h, params) => {
return h('div',
formatDate(new Date(params.row.title), 'yyyy-MM-dd hh:mm')
// Date是后台时间戳参数字段
// 'yyyy-MM-dd hh:mm' 对应的时间格式 2018-12-21 18:46
// 格式可以自行修改,例如 'yyyy-MM-dd' -> 2018-12-21, 'yyyy-MM' -> 2018-12
)
}
}
https://blog.csdn.net/weixin_43233914/article/details/85168854