formatTime.js
// 规定时间格式
export const formatTime = function (date) { // date为 Date对象
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// 规定数字格式
function formatNumber (n) {
n = n.toString()
return n[1] ? n : '0' + n
}
复制代码
使用
import { formatTime } from '../utils/formatTime'
formatTime(new Date(1555300800000)) // 2019/04/15 12:00:00
复制代码
转载于:https://juejin.im/post/5cb3f4a1e51d456e770bdca4