Compare 2 dates in HUBL

SOLVE
PNUSSBAUMER
Member

Hi there

 

I have the following challenge: I want to publish something if the date (Text Field from a module) is bigger than the current date. So I tried this:

{% set publish_date = widget.publish_end_date %}
{% set current_date = local_dt|datetimeformat('%d.%m.%Y') %}

{% if publish_date >= current_date %}

 

 

... some HTML Code

{% endif %}

 

But it doesn't work. Doese anybody has an idea. I checked similar post but nothing helped me. 

 

Thanks a lot,

philip

0 Upvotes
2 Accepted solutions

Accepted Solutions
Jsum
Solution
Key Advisor

@PNUSSBAUMER,

 

Where is "widget.publish_end_date" coming from? if you are referencing a custom module you set up on the post page to hold an manually inserted end date for an event then you are not referencing it correctly. It would be "widget_data.publish_end_date.value" .value being the value holder for a text field, .html for richtext, etc. 

 

I would check the output of both date types that you are checking. They both need to be integers in order for you to compare them by size. If both dates correctly referenced, and are correctly converted to integers then comparing their size will do what you need.

View solution in original post

0 Upvotes
Jsum
Solution
Key Advisor

Look at unixtimestamp at the bottom of this page

 

Here is the definition of Unix time time in Wikipedia.

 

basically it's the number of seconds between 00:00:00 1 January 1970 and a given date, in your case you want the number of seconds between that date and your two dates to compare. convert both to unix time.

 

Does widget.publish_end_date ouput anything? that seams wierd to me. If you click the sprocket in the to right corner on a page using this template and view developer info fomr the list you will be taken to a page that looks a little like view source. you can use ctrl +f to search for "publish_end_date" and you will find the data for that module which you can use to build the token. 

Your using export_to_template_context=True right?

View solution in original post

0 Upvotes
4 Replies 4
Jsum
Solution
Key Advisor

@PNUSSBAUMER,

 

Where is "widget.publish_end_date" coming from? if you are referencing a custom module you set up on the post page to hold an manually inserted end date for an event then you are not referencing it correctly. It would be "widget_data.publish_end_date.value" .value being the value holder for a text field, .html for richtext, etc. 

 

I would check the output of both date types that you are checking. They both need to be integers in order for you to compare them by size. If both dates correctly referenced, and are correctly converted to integers then comparing their size will do what you need.

View solution in original post

0 Upvotes
PNUSSBAUMER
Member

Thanks a lot for your answer.

 

The "widget.publish_end_date" is a field from a module with the type text with a content of "27.10.2017" as an example. I tried "widget_data.publish_end_date.value" but this is empty. 

I understand what you mean but with which method I can convert to integer? 2With "|int" or with "unixtimestamp()" or so?

 

Thanks again. Very helpful.

0 Upvotes
Jsum
Solution
Key Advisor

Look at unixtimestamp at the bottom of this page

 

Here is the definition of Unix time time in Wikipedia.

 

basically it's the number of seconds between 00:00:00 1 January 1970 and a given date, in your case you want the number of seconds between that date and your two dates to compare. convert both to unix time.

 

Does widget.publish_end_date ouput anything? that seams wierd to me. If you click the sprocket in the to right corner on a page using this template and view developer info fomr the list you will be taken to a page that looks a little like view source. you can use ctrl +f to search for "publish_end_date" and you will find the data for that module which you can use to build the token. 

Your using export_to_template_context=True right?

View solution in original post

0 Upvotes
PNUSSBAUMER
Member

Thanks a lot, again for your help.

 

Thats the status at the moment:

{% set publish_date_data = widget_data.publish_end_date.value|datetimeformat('%d.%m.%Y') %}
{% set publish_date = unixtimestamp(publish_date_data) %}
{% set current_date = unixtimestamp(local_dt) %}

{% if publish_date >= current_date %}

...
{% endif %}

 

To your questions: 
- Yes, "widget_data.publish_end_date.value" gives me "20.10.2017" back because "widget.publish_end_date" its a text field and I typed in that.
- So I wanted to make a datetimeformat out of it. That runs well. The outpout of publish_date_data is "20.10.2017".

But then the second line where I want to calculate the unixtimestamp out of it to compare with the unixtimestamp of "local_dt" gives me that Error back "TemplateSyntaxException: Error invoking function 'unixtimestamp'".

 

Other ideas? Sorry 😉

 

0 Upvotes