Knowledge to determine ------ js Good morning, good morning, good afternoon, good evening, good evening

➢ Time Status


Good judgment js morning, good morning, good afternoon, good evening, good evening

➢ analytical principle


To determine the current time client or the server is returned.

H acquisition time value (24 hours), and returns the corresponding period by determining whether the text

➢ Defined Functions

The definition of a function 

let 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 = `晚上好`;
    }
    console.log(`hours >>>>>`, hours);
    console.log(`text >>>>`, text);
    // 返回当前时间段对应的状态
    return text;
};


You can use when needed

let textState=getTimeState()
 

 

Published 163 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/COCOLI_BK/article/details/103422712