Personalized user greeting based on time of day

Is there a more elegant way to accomplish this with less columns? I want to display a personal greeting to users on the home screen based on the time of day like:

:wave: Good morning, Jake!

I’ve got the TOD handled using a Math column, the challenge is how to handle the punctuation of the greeting when a user hasn’t added a username yet? As you can see in the screenshots below, I’m using a Template column to create the greeting based on TOD and then using ITE columns to remove the extraneously punctuation when usernames don’t exist.

There’s got to be a better way, right? It shouldn’t take 8 columns to accomplish this. Plus, I would prefer to have the greetings editable instead of hard-coded as parameters of a computed column. Ugh.

Any insight would be greatly appreciated. Thx

You don’t need separate if-then-else columns for each of morning/afternoon/evening. You can replace those with a single if-then-else column, something like:

  • If TOD is less than 12, then Morning
  • Else if TOD is less than 18, then Afternoon
  • Else Evening

For the minimum number of columns, you could probably just pass the User’s name (or an empty string if no username) to a JavaScript column, and encapsulate all logic in that single column.

3 Likes

If you are using JavaScript, you only need one column.

function getTimeOfDay(p1) {
    const hours = p1.getHours();
    return (hours >= 5 && hours < 12) ? "Morning" :
           (hours >= 12 && hours < 17) ? "Afternoon" :
           (hours >= 17 && hours < 20) ? "Evening" : "Night";
}

function greet(p1, p2) {
    const timeOfDay = getTimeOfDay(p1);
    return p2 ? `👋 Good ${timeOfDay}, ${p2}!` : `👋 Good ${timeOfDay}!`;
}

return greet(new Date(), p2);

2 Likes

Yeah, I’d probably do something similar. Although passing the current time is redundant, especially when you call the function with new Date() :man_shrugging:

Here is a modified version that only requires the user name as a single parameter:

function getTimeOfDay() {
    const hours = new Date().getHours();
    return (hours >= 5 && hours < 12) ? "Morning" :
           (hours >= 12 && hours < 17) ? "Afternoon" :
           (hours >= 17 && hours < 20) ? "Evening" : "Night";
}

function greet(p1) {
    const timeOfDay = getTimeOfDay();
    return p1 ? `👋 Good ${timeOfDay}, ${p1}!` : `👋 Good ${timeOfDay}!`;
}

return greet(p1);

5 Likes

Including Current Time as a parameter, but not actually using it or referring to it in the code, does have an advantage because it will cause the javascript to run every 10 seconds and continuously update instead of caching. Could be useful if the app is open and the time flips between morning, afternoon, or evening.

4 Likes

@Jeff_Hager To clarify your point, if I’m using a Glide table in this instance and leave the Current Time parameter in as @Himaladin suggests this won’t incur any updates or syncs that would count against my plan’s monthly quota, correct?

No, Glide only considers updates for actions: add, edit, delete, or sync with third parties.

3 Likes

@Darren_Murphy @Himaladin @Jeff_Hager Just another example of why I like the Glide community so much — everyone is so generous with their time and willing to share their knowledge. A big thank you to all of you to rein in my exorbitant amount of columns. :laughing: Much appreciated! :100:

3 Likes

@Himaladin That’s what I thought, thx for confirming.

1 Like

@Darren_Murphy @Himaladin Quick follow up question on this JavaScript. Late last night I noticed the “Good morning” greeting was displaying before midnight. Since I want to the greeting to cover all hours of the day, I changed it to:

function getTimeOfDay() {
    const hours = new Date().getHours();
    return (hours >= 00 && hours < 12) ? "Morning" :
           (hours >= 12 && hours < 17) ? "Afternoon" :
           (hours >= 17 && hours < 24) ? "Evening" : "Night";
}

function greet(p1) {
    const timeOfDay = getTimeOfDay();
    return p1 ? `👋 Good ${timeOfDay}, ${p1}!` : `👋 Good ${timeOfDay}!`;
}

return greet(p1);

I’m confident the evening greeting will behave correctly, but want to see if my use of “00” for the morning greeting will run or should it be “0”? My experience with a 24-hour clock is that “00” is how the midnight hour is referenced but not sure if JavaScript will understand it that way? TIA

It’s a number, so theoretically it should only be 0.

1 Like

Thx @ThinhDinh. Guess I was overthinking this since the Time of Day Math column in Glide will display “0” during the midnight hour. :laughing:

1 Like

Do you have a different concept of evening and night time?

No, just want to make sure the greeting is accurate all day and all night. :grinning: This is a public app and we’ll have users active in the app late at night and super early in the morning depending on their time zone.

Thank you again for the JavaScript solution to this, something I definitely couldn’t have done on my own. :+1:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.