How to compare date with current date in Jinja2

Hi,
I am trying to write a code in for Email Alert, were I want to compare the Current Date with a date field, any idea how to compare current date with a date field using Jinja2?

Regards
Ruchin Sharma

1 Like

@ruchin78,

utils.date_diff(start_date, end_date) == 0 should work. date_diff will calculate and return the date difference between two dates

@makarand_b what about current date?

use utils.today()

@makarand_b
Its not working when I am trying this it gives me error:

Error while evaluating Email Alert . Please fix your template.

Here is my code:

{%-  for data in doc.cft_contact_points -%}
{% if utils.date_diff(utils.today(), data.modified) == 0 %}
{{ data.get_formatted("modified") }}   {{ data.last_contacted_by }}    {{ data.last_contact_by_name }}   {{ data.last_contact_date }}   {{ data.last_contact_type }}
 <br>
{% endif  %}
{%- endfor -%}

If I omit the if condition, it works fine.

Regards
Ruchin Sharma

I wonder there is now way to compare a given date with the current date using Jinja.

@makarand_b
@saurabh6790
Here is the solution of my problem:

{% if  frappe.utils.formatdate(frappe.utils.today(), 'dd-MM-YYYY') == frappe.utils.formatdate(data.modified, 'dd-MM-YYYY') %}
frappe.utils.formatdate(frappe.utils.today(), 'dd-MM-YYYY') represents the current date and convert it into DD-MM-YYYY Format:
frappe.utils.formatdate(data.modified, 'dd-MM-YYYY') represents the modified date of the child table.

Below is my Complete Code:

<table class="table table-bordered">
  <thead>
    <tr>
      <td style="width: 20%">Modified on</td>
      <td style="width: 20%">Contacted By</td>
      <td style="width: 20%">Contacted To</td>
      <td style="width: 20%">Contact Date</td>
      <td style="width: 20%">Contact Type</td>
    </tr>
  </thead>
  <tbody>
{%-  for data in doc.cft_contact_points -%}
{% if  frappe.utils.formatdate(frappe.utils.today(), 'dd-MM-YYYY') == frappe.utils.formatdate(data.modified, 'dd-MM-YYYY') %}

<td> {{ data.get_formatted("modified") }} </td>
<td> {{ data.last_contacted_by }} </td>
<td> {{ data.last_contact_by_name }} </td>
<td> {{ data.last_contact_date }} </td>
<td> {{ data.last_contact_type }} </td>
{%- endif -%}
 </tbody>
</table>
{%- endfor -%}

Regards
Ruchin Sharma

3 Likes