• 主页
  • 使用moment.js确定当前时间(以小时为单位)是否在特定时间之间

使用moment.js确定当前时间(以小时为单位)是否在特定时间之间

我正在开发一个功能,可以用时间感知的问候(早上好,下午好,晚上好,晚上好)向用户问候。这是我写的脚本

import moment from "moment";

function generateGreetings(){
    if (moment().isBetween(3, 12, 'HH')){
        return "Good Morning";
    } else if (moment().isBetween(12, 15, 'HH')){
        return "Good Afternoon";
    }   else if (moment().isBetween(15, 20, 'HH')){
        return "Good Evening";
    } else if (moment().isBetween(20, 3, 'HH')){
        return "Good Night";
    } else {
        return "Hello"
    }
}

$("greet")
.css({
    display: "block",
    fontSize: "4vw",
    textAlign: "center",
    })
.text(generateGreetings() +", name")

但它根本不起作用,只是返回"Hello“。我也尝试过使用

var currentTime = moment();
var currentHour = currentTime.hour();

并在函数内部使用currentHour替换moment(),但当我这样做时,站点就消失了。希望这里的任何人都能对我应该做些什么来解决这个问题有任何见解。

转载请注明出处:http://www.jubohx.com/article/20230510/2364365.html