格式化日期对象,返回 yyyy-MM-dd HH:mm:ss 的形式
function formatDate(date) {
//判断参数data是否是日期对象
if (!(date instanceof Date)) {
console.error('date不是日期对象')
return;
}
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 hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
var d = new Date()
console.log(formatDate(d))