Is it possible to compare a post's date to now?

I’ve been using the Archive code

{% for year, posts in section.pages | group_by(attribute="year") %}
    <h2>{{ year }}</h2>

    <ul>
    {% for post in posts %}
        <li><a href="{{ post.permalink }}">{{ post.title }}</a></li>
    {% endfor %}
    </ul>
{% endfor %}

to list the blog posts I’ve written and it works great.

However, now I want to filter out posts that are post-dated (having a date property in the future). This way I could do scheduled releases without having to use draft and redeploying.

Any ideas?

1 Like

I did not find anything more straightforward than this:

{% for page in section.pages %}
	{% if page.date %}
		{% set page_time = page.date|date(format="%s")|int %}
		{% set curr_time = now()|date(format="%s")|int %}
		{% if page_time < curr_time %}
		Hello from the past
		{% else %}
		Hello from the future
		{% endif %}
	{% endif %}
{% endor %}

But it works.

3 Likes

Probably the only way to do it currently yes

2 Likes

It would be nice to add a timestamp filter to Tera to allow something like:

{% if article.date | timestamp > now(timestamp=true) %}

By the way, Tera supports now() as timestamp : https://keats.github.io/tera/docs/#now

And of course it would be nice to have native date comparison :

{% if article.date > now(timestamp) %}

@keats it would be great to have a simple working date comparison mechanism. And/or provide some strings comparison mechanism.