写法一:
/**
* @description 获取当前日期
* @param
* @returns String
*/
export function getNowTime() {
let dateTime
let yy = new Date().getFullYear()
let MM = new Date().getMonth() + 1
let dd = new Date().getDate()
let hour = new Date().getHours()
let minutes = new Date().getMinutes()
let seconds = new Date().getSeconds()
if (MM < 10) {
MM = '0' + MM
}
if (dd < 10) {
dd = '0' + dd
}
let hh = hour < 10 ? '0' + hour : hour
let mm = minutes < 10 ? '0' + minutes : minutes
let ss = seconds < 10 ? '0' + seconds : seconds
dateTime = yy + '-' + MM + '-' + dd + 'T' + hh + ':' + mm + ':' + ss
console.log(dateTime)
return dateTime
}
写法二:
/**获取当前系统时间 */
export function formatTime() {
let dateTime
let date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
dateTime = [year, month, day].map(formatNumber).join('/') +
' ' + [hour, minute, second].map(formatNumber).join(':')
return dateTime
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}