js工具函数之时间转换(统一转换为“年-月-日 时-分-秒“格式)

 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(要处理的参数)  //使用--和接口使用类似

猜你喜欢

转载自blog.csdn.net/weixin_44191318/article/details/130988031
今日推荐