[SOLVED] How do you extract current "hour"? + Dynamic greeting message based on the time of the day

For anyone who might reference this, here’s the code for the dynamic greeting message based on the time of the day. Thanks to @Mihaly_Toth @Kirill_Leventcov, @Fred_Kuzyk, and @Raphael_WALTER

IF(“05”<FORMAT_DATETIME_LOCAL(NOW(),“hh”)&&FORMAT_DATETIME_LOCAL(NOW(),“hh”)<“11”, “Good Morning”, IF(“11”<NOW(“hh”)&&NOW(“hh”)<“17”, “Good Afternoon”, “Good Evening”)) + “!”


Hello everyone! I’m making a greeting message that changes based on the time of the day.

My thought process is to use NOW(“”) to get the current date and time, and use GET_DATETIME_COMPONENT(DATE, “hour”) to get the hour. However, the NOW(“”) won’t work with the GET_DATETIME_COMPONENT function.

Would you help me how I can extract “hour” from current time?

Thanks!

Simply create a variable, for example App.DATE_TIME (as a Date/time text variable)
Set it to NOW()
Use this in your formula.

1 Like

Or you can further simplify this flow, just use the correct syntax in the NOW() formula. In your case?

NOW("hh")

or

NOW("h")

Depending on the styling, play around with this.

1 Like

@Mihaly_Toth This worked! Thank you so much!

May I ask you one more question? I’m trying to make the IF condition to be NOW(“hh”) between 05 and 11. I used && for two conditions to be true, but the formula is not accepting the second “<” (invalid syntax: unexpected “<”

IF(NOW(“hh”) >“05” && <“11”,“Good Morning”,“Good Afternoon”)

Do you have suggestion how to tackle this?

Thanks!

You should use NOW("hh") > "05" && NOW("hh") < "15" instead. The expression && < "11" compares “11” to nothing, thus you are getting an error.

image

3 Likes

Exactly, or if you are as easily forgetting your formulas as me and want to remember why you put the relations that way:

IF("05"<NOW("hh")&&NOW("hh")<="11", "Good Morning", "Good Afternoon")

And of course, if you want to take it further, you can nest the IF conditions as follows:

IF("05"<NOW("hh")&&NOW("hh")<="11", "Good Morning", IF("11"<NOW("hh")&&NOW("hh")<="17", "Good Afternoon", "Good Evening"))

Oh yeah and the note about this relation: <=
< this means that 11:14 would already evaluate to Good Afternoon, however, you might want to tell the user Good Afternoon only after 12, so either has to set the upper limit to “12”, or do this: <= which is the greater than this includes the values up till 11:59.

And so on… Please note that if a formula is more than 4 lines long there might be issues when you click on a different line in the formula editor, so I would suggest using your arrow keys to navigate the formula editor. Also, the highlighting feature helps a lot when trying to figure out which part you are at.

2 Likes

Thanks for the explanation! I totally understood the concept now :slight_smile:

@Mihaly_Toth Thank you so much for detailed explanation and also how to use the nested IF. That was going to be my next step to include all “Good Morning,” “Good Afternoon,” and “Good Evening.” :slight_smile:

@Mihaly_Toth @Kirill_Leventcov May I ask you one more question?

I have updated my function.

IF(“05”<NOW(“hh”)&&NOW(“hh”)<“11”, “Good Morning”, IF(“11”<NOW(“hh”)&&NOW(“hh”)<“17”, “Good Afternoon”, “Good Evening”)) + “!”

I’m not getting the local time back. In the AppGyver documentation, I found the SET_TIMEZONE function, but this doesn’t allow “hh” inside of NOW() function.
SET_TIMEZONE(NOW(), LOCAL_TIMEZONE())

Another function is FORMAT_DATETIME_LOCAL(). I tried this with “hh” in it.

It did accept it, but I’m not sure whether this is actually correct.

Do you have any advice? Thank you.

Let me try to contribute here.
I would assign the NOW("hh") value to a page variable called “now”
Then try SET_TIMEZONE(pageVar.now), LOCAL_TIMEZONE())

@Fred_Kuzyk Thanks - I will try this method, too!

I was able to submit this formula! FORMAT_DATETIME_LOCAL(NOW(),“hh”)

IF(“05”<FORMAT_DATETIME_LOCAL(NOW(),“hh”)&&FORMAT_DATETIME_LOCAL(NOW(),“hh”)<“11”, “Good Morning”, IF(“11”<NOW(“hh”)&&NOW(“hh”)<“17”, “Good Afternoon”, “Good Evening”)) + “!”

Thank you @Mihaly_Toth and @Kirill_Leventcov

1 Like

I was about to write this formula as this is the “go with” super simple solution, but I am glad that you have figured it out yourself. That’s the fun part about the journey here. Good luck with the next steps.

1 Like