var me = this
var date = this.getDateByType().toString(); //获取当前年份
me.date=date //年月日 星期几形式
var timeState = this.getTimeState()
me.timeState=timeState
console.log('getTimeState', me.timeState)
getTimeState() {
// 获取当前时间
let timeNow = new Date();
// 获取当前小时
let hours = timeNow.getHours();
// 设置默认文字
let text = ``;
// 判断当前时间段
if (hours >= 0 && hours <= 10) {
text = `早上好`;
} else if (hours > 10 && hours <= 14) {
text = `中午好`;
} else if (hours > 14 && hours <= 18) {
text = `下午好`;
} else if (hours > 18 && hours <= 24) {
text = `晚上好`;
}
// 返回当前时间段对应的状态
return text;
},
getDateByType(type) {
var date = new Date();
var myyear = date.getFullYear();
var str = "星期";
var week = new Date().getDay();
switch (week) {
case 0:
str += "日";
break;
case 1:
str += "一";
break;
case 2:
str += "二";
break;
case 3:
str += "三";
break;
case 4:
str += "四";
break;
case 5:
str += "五";
break;
case 6:
str += "六";
break;
}
console.log(str)
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = '0' + mymonth;
}
if (myweekday < 10) {
myweekday = '0' + myweekday;
}
if (type === "year") {
return myyear;
} else if (type === "month") {
return myyear + '-' + mymonth;
}
return myyear + '年' + mymonth + '月' + myweekday + '日' + ',' + str;
},