Updated Date on Blog

SOLVE
jw20
Member

I am editing our blog template to include an "Updated on" date. I have followed this article (which has the code to copy missing) and it gets close, but it doesn't completely work. It works when there's an updated date, but when you first publish the post, it displays "Published on February 3, 2021, updated on February 3, 2021." I want it to only show the "updated on" if the post is updated. The code from HubSpot is below. I don't think I need the UTC offset because I'm not showing a time, but I've included it.

 

{% set update_time = content.updated %}
{% set offset = 14400000 %}
{% set new_time = update_time - offset %}
Published on {{ content.publish_date|datetimeformat('%B %e, %Y') }}
{% if content.updated|unixtimestamp > content.publish_date|unixtimestamp %}
    , updated on {{ new_time|datetimeformat('%B %e, %Y') }}
{% endif %}

 

I have also tried this - with the same result (updated date shows regardless if there was an update).

 

Published on {{ content.publish_date|datetimeformat('%B %e, %Y') }}
 {% if content.updated|unixtimestamp > content.publish_date|unixtimestamp %}
   , updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% endif %}

 

Can anyone help with the syntax for the "if" so that the "updated on" only shows if the post is updated? I usually work in php and am just learning Hubl.

0 Upvotes
2 Accepted solutions

Accepted Solutions
dennisedson
Solution
Community Manager

By some coincedence, this was also discussed on the dev slack here

Here is the code that finally worked (update with your copy)

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  Originally posted on {{ content.publish_date_localized }}
  <br>Last updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}
{% else %}
  {{ content.publish_date_localized }}
{% endif %}
Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content

View solution in original post

jw20
Solution
Member

I changed the updated date from 

{{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

to

{{ content.updated|datetimeformat('%B %e, %Y') }}

Otherwise it showed the following day.

View solution in original post

17 Replies 17
dennisedson
Community Manager

Hey @jw20 

Welcome to the Community and congrats on getting into Hubl! 

I think your time with PHP will serve you well here 😁

What I think you need is an if else statement instead of just an if statement

Read up on it here

@piersg  is super well versed in all things Hubl and can probably lend a hand!

 

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
piersg
Key Advisor

Hi @jw20 (thanks @dennisedson) and welcome! I think what you need is content.created, not content.publish_date. So:

 

Published on {{ content.created|datetimeformat('%B %e, %Y') }}
{% if content.updated|unixtimestamp > content.created|unixtimestamp %}
    , updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% endif %}

 

dennisedson
Community Manager

Oops!  Totally misread the question.  Somehow I read it as

If not updated -- show Published on date

else show Updated on date.

Apologies for my misleading info 😥

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
jw20
Member

@dennisedson - you are correct - I want it to show "Published on Feb 2" OR "Published on Feb 2, updated on Feb 3"

@piersg your version still has the "updated on" showing, even when not updated. That's the piece I need to work on.

 

I will keep trying. Thanks!

dennisedson
Community Manager

ok @jw20 , guess I am going to have to clean up @piersg 's mess 😜

{% if content.updated|unixtimestamp > content.created|unixtimestamp %}
    Updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% else %}
    Published on {{ content.created|datetimeformat('%B %e, %Y') }}
{% endif %}
Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
jw20
Member

That's a little big closer, @dennisedson  -

When I publish a new post it says "Updated on Feb 5" - except it should say "Published on Feb 5"

The old posts that have been updated are correct and show the update date.

 

Does changing the content.created to content.published make a difference? Is content.created a draft?

 

Thanks,

Julie

0 Upvotes
dennisedson
Solution
Community Manager

By some coincedence, this was also discussed on the dev slack here

Here is the code that finally worked (update with your copy)

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  Originally posted on {{ content.publish_date_localized }}
  <br>Last updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}
{% else %}
  {{ content.publish_date_localized }}
{% endif %}
Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content

View solution in original post

jw20
Member

Here is what I have:

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %} 
  Originally posted on {{ content.publish_date_local_time|datetimeformat('%B %e, %Y')}} <br>
  Last updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }} 
{% else %} 
  Published on {{ content.publish_date_local_time|datetimeformat('%B %e, %Y') }} 
{% endif %} 

Of my four posts, they work - aside from one: it was updated yesterday at 4pm Eastern time, but is showing the update date as today.

 

Does something else need added as far as timezone? Something in the first line?

 

Thank you so much for the help!

 

0 Upvotes
jw20
Member

I updated how I had the time set up and it's working perfectly.

Thanks for the assistance!

dennisedson
Community Manager

hey @jw20

Mind adding your final code here and accepting it as the solution 🙏

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
jw20
Solution
Member

I changed the updated date from 

{{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

to

{{ content.updated|datetimeformat('%B %e, %Y') }}

Otherwise it showed the following day.

View solution in original post

MLadenburg
Participant | Partner

@jw20  and @dennisedson ,

 

Thank you for your super helpful post. I'm trying to get mine to read "posted on," unless it's been updated so that it says, "updated on" - so basically overriding the "posted on."

Below is my code (largely copied and pasted from your efforts). At the moment, it posts both "posted on" and "updated on" - do you know what function would clear the original "posted on."? Thank you for any help.

 

<span class="hs-author-label">Posted on <a href="{{content.absolute_url}}">{{ content.publish_date_localized }}</a> by
                    {%if content.blog_post_author %}</span><br>
              {%if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  <br>Updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% else %}
  {{ content.publish_date_localized }}
{% endif %}
0 Upvotes
jw20
Member

@MLadenburg  -  Unfortunately, I'm not any help. We ultimately didn't add "Posted on" -  although I don't believe that I had gotten that to work either.  The blog post displays the date and when updated, posts "Updated on" and the new date.  

MLadenburg
Participant | Partner

@jw20  - Thanks for responding. Would you be willing to post the code in total? I'd love to get ours to do exactly that for the time being.

0 Upvotes
dennisedson
Community Manager

Hey @MLadenburg 

Try this:

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  Updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

{% else %}
Posted on  {{ content.publish_date_localized|datetimeformat('%B %e, %Y') }}
{% endif %}

@jw20 , what happened with yours?  I just tested and this works 🤔

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
jw20
Member

@dennisedson 

I remember that if I updated the post at 4pm it would display as the next day. I don't have time right now to dig through my notes, but neither of these worked for the "Updated" date - (I'm EST) if updated after 4pm -

{{ content.publish_date_localized|datetimeformat('%B %e, %Y') }}

 

{{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

 

I ended up using this below, but the client decided not to include the words "Posted on"

{{ content.updated|datetimeformat('%B %e, %Y') }} 

  

I'm sure it was some syntax issue on my end, but it was necessary to test across times and scenarios (like, saving a draft vs published) to double check it worked.

MLadenburg
Participant | Partner

@dennisedson  and @jw20 ,

 

Thank you for all of your help! I tried @dennisedson 's code, but was really wanting the "posted on" and "updated on" written out - so was eventually able to get our dev's eyeballs on it to help me out. I'm posting it here and a screenshot with how it looks in case anyone ever needs it!

 

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
          Updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}
            <br>Posted on {{ content.publish_date_localized }} by 
            {% if content.blog_post_author %}
              <a class="author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}">{{ content.blog_post_author.display_name }}</a>
            {% endif %}
          {% else %}
              Posted on {{ content.publish_date_localized }} by {% if content.blog_post_author %} <a class="author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}">{{ content.blog_post_author.display_name }}</a>
              {% endif %}
          {% endif %}

 

Screen Shot 2021-07-07 at 2.44.33 PM.png