Expand my Community achievements bar.

SOLVED

Check The Date Condition like Date occurs after 30 days in If condition in JSON

Avatar

Level 2

We are trying to find the right Json where check the date occurs less or greater then xx days after today. please find the below sample. Birthdate has the year init. Do we have any other option to crack this?

 

{%#if profile.person.birthDate occurs <=30 days after today%} {"content_id":"sample": "{{profile.person.name.firstName}}"} {%/if%}

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@gengaipandi11 ,

To compare dates in JSON & to check if the birthdate occurs less or greater than xx days after today, you can use-

{% if moment(profile.person.birthDate).diff(moment(), 'days') <= 30 %}

  {"content_id": "sample", "name": "{{profile.person.name.firstName}}"}

{% endif %}

In the above, moment(profile.person.birthDate) creates a moment object from the birthdate, and moment() creates a moment object for the current time. The diff method calculates the difference in days between the two moments. If the difference is less than or equal to 30, the JSON object is output with the person's first name.

Note that you may need to adjust the syntax and formatting depending on the specific JSON library or template engine you are using.

 

View solution in original post

2 Replies

Avatar

Community Advisor

Can you try this to see if that works,

 

{%#if (profile.person.birthDate.getDayOfYear() - now.getDayOfYear() <= 30) and (profile.person.birthDate.getDayOfYear() - now.getDayOfYear() >= 0) %}

{{profile.person.name.lastName}}

{%/if%}

Thanks, Sathees

Avatar

Correct answer by
Employee Advisor

@gengaipandi11 ,

To compare dates in JSON & to check if the birthdate occurs less or greater than xx days after today, you can use-

{% if moment(profile.person.birthDate).diff(moment(), 'days') <= 30 %}

  {"content_id": "sample", "name": "{{profile.person.name.firstName}}"}

{% endif %}

In the above, moment(profile.person.birthDate) creates a moment object from the birthdate, and moment() creates a moment object for the current time. The diff method calculates the difference in days between the two moments. If the difference is less than or equal to 30, the JSON object is output with the person's first name.

Note that you may need to adjust the syntax and formatting depending on the specific JSON library or template engine you are using.