Hexo: Last post update date

Created on 26 Mar 2018  ·  8Comments  ·  Source: hexojs/hexo

Hello,

In my post page I generated last update date (update.ejs):

<% if (page.updated) { %>
<div class="postdate">
<i class="fas fa-sync"></i> <time datetime="<%= date_xml(page.updated) %>" itemprop="dateUpdated"><%= page.updated.format(config.date_format) %></time>
</div>
<% } %>

it's based on original date.ejs

<% if (post.date) { %>
    <div class="<%= class_name %>">
<i class="fas fa-calendar-alt"></i> <time datetime="<%= date_xml(post.date) %>" itemprop="datePublished"><%= date(post.date, config.date_format) %></time>
    </div>
<% } %>

In my post template I added <%- partial('_partial/post/update') %>:

<%- partial('_partial/post/date', { post: page, class_name: 'postdate' }) %>
<%- partial('_partial/post/update') %>
<%- partial('_partial/post/tag') %>
<%- partial('_partial/post/category') %>

and it works fine, but I want to display updated date info only when it's different/greater than post date.
I tried to use:
<% if (page.updated) > (post.date){ %>
and
<% if (page.updated) != (post.date){ %>
but doesn't work :(

Any ideas, what I am doing wrong?

stale

All 8 comments

The data must be provided when rendering ejs containing variables, you can try

<%- partial('_partial/post/update'), { post: page } %>

Hi JLHwung,

Thanks for your help.

I added your suggestion to my post.ejs but still nothing, both dates are displayed.

I have cleanup it a little:
date.ejs

<% if (post.date) { %>
    <div class="<%= class_name %>">
<i class="fas fa-calendar-alt"></i> <time datetime="<%= date_xml(post.date) %>" itemprop="datePublished"><%= date(post.date, config.date_format) %></time>
    </div>
<% } %>

update.ejs

<% if (post.updated) { %>
<div class="<%= class_name %>">
<i class="fas fa-sync"></i> <time datetime="<%= date_xml(post.updated) %>" itemprop="dateUpdated"><%= date(post.updated, config.date_format) %></time>
</div>
<% } %>

post.ejs (here I want to display <%- partial('_partial/post/update', { post: page, class_name: 'postdate' }) %> only when its different that publish date

<article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    <%- partial('_partial/post/title', { post: page, index: false, class_name: 'posttitle' }) %>
    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name"> <% if (page.author) { %><%- page.author %><% } else { %><%- config.title %><% } %></span>
      </span>
      <%- partial('_partial/post/date', { post: page, class_name: 'postdate' }) %>
      <%- partial('_partial/post/update', { post: page, class_name: 'postdate' }) %>
      <%- partial('_partial/post/tag') %>
      <%- partial('_partial/post/category') %>
   </div>
  </header>
  <%- partial('_partial/post/gallery') %>
  <div class="content" itemprop="articleBody">
    <%- page.content %>
  </div>
</article>
<%- partial('_partial/comments') %>

If you you want to display update date only, use the following snippet

<% if (page.updated > page.date) { %>
<%- partial('_partial/post/update', { post: page, class_name: 'updatedate' }) %>
<% } else { %>
<%- partial('_partial/post/date', { post: page, class_name: 'postdate' }) %>
<% } %>

And the update.ejs

<% if (post.updated) { %>
<div class="<%= class_name %>">
<i class="fas fa-sync"></i> <time datetime="<%= date_xml(post.updated) %>" itemprop="dateUpdated"><%= post.updated.format(config.date_format) %></time>
</div>
<% } %>

Works perfect! Thank you.
Last question. How to change code to display _publish date_ always and _update date_ additionally only when this is true (page.updated > page.date). When its the same (post was not updated) I want to display only _publish date_.
Thanks in advance!

Please move partial('_partial/post/date') out of the else scope. The ejs is pretty straightforward javascript-like programs, which means you can tailor it for your requirements.

Thank you for your time. I am new in hexo, I was always using Wordpress.

Ok so I have now something like this:

      <%- partial('_partial/post/date', { post: page, class_name: 'postdate' }) %>
      <% if (page.updated > page.date) { %>
      <%- partial('_partial/post/update', { post: page, class_name: 'postdate' }) %>
      <% } else { %>
      <% } %>
      <%- partial('_partial/post/tag') %>
      <%- partial('_partial/post/category') %>

and some live example
not updated post but still both are displayed http://0ut3r.space/2018/03/27/md5-calculator-in-powershell/
and updated post which is good (both are displayed) http://0ut3r.space/2018/03/19/starting-with-hexo.io/

So in my test environment I also created clean hexo instance with two posts. (only one updated) and your suggestion:

<% if (page.updated > page.date) { %>
<%- partial('_partial/post/update', { post: page, class_name: 'updatedate' }) %>
<% } else { %>
<%- partial('_partial/post/date', { post: page, class_name: 'postdate' }) %>
<% } %>

and it always display only updated part even for the post not updated :(

This issue has been automatically marked as stale because lack of recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@h0ek Use isAfter method of Moment. It worked for me.

if(page.updated.isAfter(page.date, 'day'))

The day parameter specifies to return true only if the updated time is newer by at least a day. By default it compares by miliseconds.

Was this page helpful?
0 / 5 - 0 ratings