Different Greeting for time of day

Answered

15 Comments

  • Graeme Carmichael
    Community Moderator

    Colin

    You can use liquid markup to customise your greeting.

    Try this:

    {% assign hour= 'now' | date:'%k' | plus: 0 %}

    This is the hour of day {{hour}}

    {% if hour < 12 %} Good Morning {% else %} Good Afternoon.{% endif %}

    You will have to vary the code depending on daylight saving. Unfortunately, I do not know how to handle that automatically.

     

    0
  • Colin Campbell

    That's great, thanks so much! 

    0
  • Andrew J
    Community Moderator

    Hello Graeme,

    What about widening the variations as below - it's not working - I think my logic is wrong...

    {% assign hour= 'now' | date:'%k' | plus: 13 %}{% if hour < 10 %}morning{% if hour < 12 %}day{% if hour < 16 %}afternoon{% else %}evening{% endif %}

    0
  • Andrew J
    Community Moderator

    Looks like I got it, needed to use 'case'

    {% assign hour= 'now' | date:'%k' | plus: 13 %}{% case hour %} 
    {% when 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 %} morning
    {% when 10 or 11 or 12 or 13 %} day
    {% when 14 or 15 or 16 %} afternoon
    {% else %} evening
    {% endcase %}
    0
  • Mark Weissman

    After a bunch of trial and error, I got the following:

    Good {% assign hour = 'now' | date:'%k' | plus: -4 %} {% if hour < 12 %} morning {% elsif hour < 16 %} afternoon {% else %} evening {% endif %} {{ticket.requester.first_name}},

     

    Here's how I understand the markup:

    {% assign hour = 'now' | date:'%k' | plus: -4 %}

    converts the word "hour" into a number.  That number is the current hour, based on a 24 hour clock:

    'now' will pull up the time when used before "| date:"

    Adding " '%k'  " will make it only show the hour

    Adding "| plus: -4 " will subtract 4 hours from the previous number. This is because my time zone is GMT -4.

     

    (As Graeme Carmichael wrote:

    "This is the hour of day {{hour}}"

    Put {{hour}} in under your code to see the number that shows up.)

    At this point it's a simple if statement.

    {% if hour < 12 %} morning {% elsif hour < 16 %} afternoon {% else %} evening {% endif %}

    0
  • Graeme Carmichael
    Community Moderator

    Mark

    That is excellent. Thank you.

    0
  • Karen

    Hi All,

    I am looking to include some liquid markup so that the current day can be displayed.

    I have worked out how to have the greeting show for the correct time of day, but displaying the day is alluding me.

    Just to say "Happy Wednesday" or something like that.

    Any help on this would be great.

    Karen

    0
  • Graeme Carmichael
    Community Moderator

    Karen

    Try this:

    Happy {{ 'now' | date: '%A' }} !

     

    0
  • Karen

    Thanks so much @Graeme

    0
  • Jeremy Heath

    @Graeme Carmichael

    I've used this for a while from a Macro and it has been fine, but lately it has been adding extra spaces between the Good >Morning> Name part.

    I've not changed anything, here is my code.

    Good {% assign hour= 'now' | date:'%k' | plus: 0 %} {% if hour < 11 %} morning {% elsif hour < 17 %} afternoon {% else %} evening {% endif %} {{ticket.requester.first_name}},

    Any idea why it behaves differently now?

    You can see the extra gaps in the image below

    0
  • Jeremy Heath

    With the help of the support desk we traced this to changes in Zendesk that were to correct earlier issues the correct code that works should be 

    Good{% assign hour= 'now' | date:'%k' | plus: 0 %} {% if hour < 11 %}morning{% elsif hour < 17 %}afternoon{% else %}evening{% endif %} {{ticket.requester.first_name}},
    0
  • Brett Bowser
    Zendesk Community Manager

    Thanks for taking the time to share this with everyone Jeremy :)

    0
  • Tim Blaum

    Enjoying this code quite a bit, thanks for sharing everyone. I am wondering if there's a way to automatically adjust between -5 for EST and -4 for EDT or whether this needs to be a manual change at every time change? We have used this in a large number of macros, and it'd be helpful not to have to edit each macro every time change, if possible.

    Good{% assign hour= 'now' | date:'%k' | plus: -5 %} {% if hour < 12 %}morning{% elsif hour < 16 %}afternoon{% else %}evening{% endif %} {{ticket.requester.first_name}},
     
    0
  • Andrew J
    Community Moderator

    I wonder if we could use options for date and program in dates for one option or the other. Could be interesting 😜

    0
  • Mark Weissman

    @... I'm guessing it's possible, but to me, it's not worth the headache when I could use "Hi" for that 1 hour instead.

    @... Try this, using {{GreetTime}} as a variable:

    ## This will set the greeting to "Good morning", "Hi", Good afternoon" or Good evening" based on EST
    {% assign hour = 'now' | date:'%k' | plus: -5 -%}{{!
    }}{% case hour %}{{!
    }}{%- when < 11 %}{{!
    }}{% assign GreetTime = 'Good morning' %}{{!
    }}{% when 11 %}{{!
    }}{% assign GreetTime = 'Hi' %}{{!
    }}{% when 12 or 13 or 14 or 15 or 16 or 17 or 18 %}{{!
    }}{% assign GreetTime = 'Good afternoon' %}{{!
    }}{% else %}{{!
    }}{% assign GreetTime = 'Good evening' %}{{!
    }}{% endcase %}
    0

Please sign in to leave a comment.

Powered by Zendesk