Lewis Collins
Lewis Collins

Reputation: 53

django template print either "good morning or afternoon" depending on time of day

Morning,

I'm trying to create a template for an email and I'm unsure on how to get the greeting to flip between "Morning, afternoon and evening" depending on time of day that it is selected to be sent.

Any ideas

current template code

{% if hour < 12 %}
    morning
{% elif hour > 12 %}
   afternoon
{% endif %}

Upvotes: 0

Views: 1539

Answers (2)

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13733

You can use now in your template like this:

{% now "H" as current_time %}

{% if current_time > 12 %}
    afternoon
{% else %}
    morning
{% endif %}

Upvotes: 4

Rodriguez David
Rodriguez David

Reputation: 571

I would do something like this:

{% if now < midday %}  
    morning
{% else %}
   afternoon
{% endif %}

Where midday is defined as:

midday= datetime.time(12)

Of course that that midday must be part of your template context!

https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#now
How to check if a date time is before midday

Upvotes: 0

Related Questions