// 获取当前日期
nowTime () {
const time = new Date()
console.log (time) // Fri Aug 10 2018 21:28:59 GMT+0800 (中国标准时间)
}
// 日期格式化
const newTime = time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate()
console.log(newTime ) // 2018-8-10
// 月份和日 不足两位数时补0
// 定义 appendZero 方法
appendZero (obj) {
if (obj < 10) {
return '0' + obj
} else {
return obj
}
}
const tt = time.getFullYear() + '-' + (appendZero(time.getMonth() + 1)) + '-' + appendZero(time.getDate())
console.log(tt) // 2018-08-10
// 也可以用三元表达式的方式
appendZero (obj) {
return lut = obj < 10 ? "0" + obj : obj
}