编写代码,将当前日期按“2017-12-27 11:11 星期三”格式输出
1. 实现方式一: switch结构实现
function myDateFor1() {
let nowTime = new Date();
let minutes = nowTime.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
let dateFor = `${nowTime.getFullYear()}-${nowTime.getMonth() + 1}-${nowTime.getDate()} ${nowTime.getHours()}:${minutes} `;
switch (nowTime.getDay()) {
case 0:
console.log(dateFor+'星期一');
day = '';
break;
case 1:
console.log(dateFor+'星期二');
break;
case 2:
console.log(dateFor+'星期三');
break;
case 3:
console.log(dateFor+'星期四');
break;
case 4:
console.log(dateFor+'星期五');
break;
case 5:
console.log(dateFor+'星期六');
break;
case 6:
console.log(dateFor+'星期日');
break;
default:
console.log('星期输入不对哦!');
}
}
2. 实现方式二: 数组索引取值
function myDateFor() {
let days = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
let nowTime = new Date();
let minutes = nowTime.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
let day = nowTime.getDay();
day = days[day];
console.log(`${nowTime.getFullYear()}-${nowTime.getMonth()}-${nowTime.getDate()} ${nowTime.getHours()}:${minutes} ${day}`);
}
myDateFor();