1.在utils文件夹引入文件data.js
const date = {
/**
* 输入 *
* "yyyy/MM/dd","yyyy-MM-dd", "yyyyMMdd","yyyy年MM月dd日"
* "yyyy年MM月dd日 HH时mm分ss秒", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss"
*
* 输出 yyyy-MM-dd HH:mm:ss
* @param date
* @param fromType
* @returns {string}
*/
formatterDate: function(date, fromType) {
let DateTime = 'yyyy-MM-dd HH:mm:ss'
let index = -1
let lastIndex = ''
// y
index = fromType.indexOf('y')
if (index > -1) {
lastIndex = index + 4
DateTime = DateTime.replace('yyyy', date.substring(index, lastIndex))
} else {
DateTime = DateTime.replace('yyyy', '2018')
}
// M
index = fromType.indexOf('M')
if (index > -1) {
lastIndex = index + 2
DateTime = DateTime.replace('MM', date.substring(index, lastIndex))
} else {
DateTime = DateTime.replace('MM', '01')
}
// d
index = fromType.indexOf('d')
if (index > -1) {
lastIndex = index + 2
DateTime = DateTime.replace('dd', date.substring(index, lastIndex))
} else {
DateTime = DateTime.replace('dd', '01')
}
// H
index = fromType.indexOf('H')
if (index > -1) {
lastIndex = index + 2
DateTime = DateTime.replace('HH', date.substring(index, lastIndex))
} else {
DateTime = DateTime.replace('HH', '00')
}
// m
index = fromType.indexOf('m')
if (index > -1) {
lastIndex = index + 2
DateTime = DateTime.replace('mm', date.substring(index, lastIndex))
} else {
DateTime = DateTime.replace('mm', '00')
}
// s
index = fromType.indexOf('s')
if (index > -1) {
lastIndex = index + 2
DateTime = DateTime.replace('ss', date.substring(index, lastIndex))
} else {
DateTime = DateTime.replace('ss', '00')
}
return DateTime
},
/**
标准日期:2017-09-19 或 2017-09-19 20:00:00
中国标准时间:Mon Oct 23 2017 17:20:13 GMT+0800 (中国标准时间)
时间戳:1508750413
毫秒数:1508750413000
注意:时间戳*1000就是毫秒数
时间搓每天间隔: 86400000
*/
/**
* 毫秒数或中国标准时间转日期
* @param msec
* @returns {{hasTime: string, withoutTime: string}}
*/
msToDate: function(msec) {
const result = {
hasTime: '',
withoutTime: ''
}
if (!StringUtils.isNotBlank(msec)) {
return result
}
const datetime = new Date(msec)
const year = datetime.getFullYear()
const month = datetime.getMonth()
const date = datetime.getDate()
const hour = datetime.getHours()
const minute = datetime.getMinutes()
const second = datetime.getSeconds()
/**
* 日期加时间
* @type {string}
*/
const result1 = year +
'-' +
((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) +
'-' +
((date + 1) <= 10 ? '0' + date : date) +
' ' +
((hour + 1) <= 10 ? '0' + hour : hour) +
':' +
((minute + 1) <= 10 ? '0' + minute : minute) +
':' +
((second + 1) <= 10 ? '0' + second : second)
/**
* 日期
* @type {string}
*/
const result2 = year +
'-' +
((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) +
'-' +
((date + 1) <= 10 ? '0' + date : date)
result.hasTime = result1
result.withoutTime = result2
return result
},
formatDateTime3: function(time, format){
var t = new Date(time);
var tf = function(i){return (i < 10 ? '0' : '') + i};
return format.replace(/yyyy|MM|DD|HH|mm|ss/g, function(a){
switch(a){
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'DD':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'ss':
return tf(t.getSeconds());
break;
}
})
}
}
export default date
2.在页面引入
import date from '@/utils/date.js'
3.页面使用
标签中:
{{formatterDate(item.updatedTime, 'yyyy-MM-DD')}}
methods:
// 日期转换
formatterDate(time, FormData) {
if (time) {
return date.formatDateTime3(time, FormData).replace(/-/g, '-')
}
}