function getCurrentDate(type, date) {
//如果有传入时间戳参数,则使用参数,否则使用当前时间
const date1 = date ? new Date(date) : new Date();
//转换年
let y = date1.getFullYear();
//转换月,需要自身+1
let M = date1.getMonth() + 1;
//转换日
let d = date1.getDate();
//转换时
let HH = date1.getHours();
//转换分
let mm = date1.getMinutes();
//转换秒
let ss = date1.getSeconds();
//数组补零
M = M > 9 ? M : '0' + M ;
d = d > 9 ? d : '0' + d ;
HH = HH > 9 ? HH : '0' + HH ;
mm = mm > 9 ? mm : '0' + mm ;
ss = ss > 9 ? ss : '0' + ss ;
//按照类型组合字符串,此处不局限于这两种,可按需求任意修改
if (type == 'yyyy-MM-dd') {
return `${y}-${M}-${d}`;
} else if (type == 'yyyy-MM-dd HH:mm:ss'){
return `${y}-${M}-${d} ${HH}:${mm}:${ss}`;
}
}