1、在工具包中建立js文件进行导出(一般建在utils文件夹下)
export function formatDate(cellValue) {
if (cellValue == null || cellValue == "") return ""; //空值直接返回空
var date = new Date(cellValue)
var year = date.getFullYear()
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
2、在需要使用的文件中引入并使用(如下:)
import { formatDate } from "@/utils/index" //引入
let time = formatDate(要处理的参数) //使用--和接口使用类似