Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  24] [ 1]  / answers: 1 / hits: 5986  / 4 Years ago, fri, may 29, 2020, 12:00:00

I am working on a function that'd greet its users with a time-aware greeting (Good Morning, Afternoon, Evening, Night). Here's the script that I've made



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)


But it simply wont work and just returns Hello. I've also tried using



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


and use currentHour to replace moment() inside the function but when I do so the site just dissapears.
Hoping anyone here has any insight on what I should do to fix this issue.


More From » time

 Answers
2

You are using moment().isBetween() in a wrong way. You can see the correct method usage from here. For your requirement, no need to use this isBetween method. You can simply get the hour and then check it against the if condition.


You can re-arrange your method like below.


function generateGreetings(){

var currentHour = moment().format("HH");

if (currentHour >= 3 && currentHour < 12){
return "Good Morning";
} else if (currentHour >= 12 && currentHour < 15){
return "Good Afternoon";
} else if (currentHour >= 15 && currentHour < 20){
return "Good Evening";
} else if (currentHour >= 20 || currentHour < 3){
return "Good Night";
} else {
return "Hello"
}

}

[#3654] Wednesday, May 27, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;