js判断早上好,上午好,下午好,傍晚好,晚上好

版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接 如有问题, 可发送邮件咨询. https://blog.csdn.net/weixin_37865166/article/details/89477683

原文地址: https://dsx2016.com/?p=456

➢ 时间状态

js判断早上好,上午好,下午好,傍晚好,晚上好

➢ 解析原理

根据当前客户端时间或者服务器返回的时间来判断.

获取时间中的小时的值(24小时),通过判断时间段返回对应的文本

➢ 定义函数

定义一个函数

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;
};

需要的时候使用即可

let textState=getTimeState()

猜你喜欢

转载自blog.csdn.net/weixin_37865166/article/details/89477683