Empower content timeliness: The efficient way to display the publication date of article lists in Anqi CMS.

In today's ever-changing digital world, the timeliness of content is crucial for the attractiveness of websites and user experience.Clearly displaying the publication date can help readers quickly judge the value of the information, enhancing the professionalism of the website, whether it is news reports, technical articles, or product updates.stampToDateTags, which cleverly convert the timestamp data from the background to the date format we are familiar with.

As an experienced website operations expert, I know the value of transforming technical details into practical operations. Next, I will guide everyone on how to utilize the Anqi CMSstampToDateTags, elegantly display the publication date of each document on your article list page.

Understanding the 'secret' behind time:stampToDateHow tags work

In the AnQi CMS backend, the article publication time is usually stored in the form of a 'timestamp' (Unix timestamp).This essentially is a long sequence of numbers, representing the number of seconds elapsed from 00:00:00 UTC on January 1, 1970, to a specific point in time.This format is very efficient for computers, but it seems cryptic to humans.

To solve this problem, Anqi CMS providesstampToDateThis powerful template tag.Its function, as the name implies, is to 'stamp' this numeric timestamp and convert it into a human-readable date and time format.

{{ stampToDate(时间戳, "格式") }}

There are two key parts:

  1. Timestamp: You need to pass a 10-digit or 13-digit number representing the article's publish time.
  2. FormatThis is a string that tellsstampToDateWhat format would you like the date to be displayed in? It is especially important to note that the date format string in AnQi CMS (developed based on Go language) is based on a fixed reference date2006-01-02 15:04:05defined. For example, if you want to display the format "year-month-day", you should write it as"2006-01-02"; then if you want to display the format "hour:minute", you should write it as"15:04"This reference date acts as a template. You can input the year, month, day, hour, minute, and second you want to display at its corresponding position.

Capture the moment of article publication:archiveListofCreatedTime

When we are on the article list page of Anqi CMS (usually corresponding to the modellist.htmltemplate file, for examplearticle/list.html)usingarchiveListLabel display of articles, each article's detailed information will be assigned to a temporary variable (usually named)item). In thisitemIn the object, it includes the publishing time information we need.CreatedTime.

item.CreatedTimeIt is that 10-digit timestamp that reliably records the moment the article was created and published. With it, we can use it asstampToDatethe first parameter of the tag.

Meld them together cleverly: Display the publish date on the article list page

Now, we have the "Conversion Tool" (stampToDate) and "Original Data" (item.CreatedTime),next is to combine them into your template code.article/list.html)The structure of your article list page template (e.g.

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <article class="article-item">
            <h3 class="article-title"><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <div class="article-meta">
                <span class="publish-date">发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>
                <span class="article-views">阅读量:{{ item.Views }}</span>
            </div>
            <p class="article-description">{{ item.Description }}</p>
            <a href="{{ item.Link }}" class="read-more">查看详情</a>
        </article>
    {% empty %}
        <p class="no-content">暂无文章发布。</p>
    {% endfor %}
{% endarchiveList %}

{# 如果您的列表需要分页,可以在此处添加分页标签 #}
{% pagination pages with show="5" %}
    {# 分页导航的代码,例如: #}
    <div class="pagination-nav">
        {% if pages.PrevPage %}<a href="{{ pages.PrevPage.Link }}">上一页</a>{% endif %}
        {% for page in pages.Pages %}<a href="{{ page.Link }}" class="{% if page.IsCurrent %}active{% endif %}">{{ page.Name }}</a>{% endfor %}
        {% if pages.NextPage %}<a href="{{ pages.NextPage.Link }}">下一页</a>{% endif %}
    </div>
{% endpagination %}

In the above code snippet, we specifically added in the<div class="article-meta">This area has added a line:

<span class="publish-date">发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>

This line of code is the core. When the page is accessed,archiveListit will iterate through each article,item.CreatedTimeand extract the timestamp of the current article, thenstampToDateLabels will be displayed according to your specification"2006年01月02日"in the intuitive format of "XXXX year XX month XX day". You can adjust it as neededspanThe style of the label, allowing date information to better integrate with your website design.

Flexible and versatile: customize your date display format

stampToDateThe power of tags lies in the flexibility of their format strings. Besides the example above,"2006年01月02日"you can try various combinations to meet different design and information display needs:

  • Show year and month only:{{ stampToDate(item.CreatedTime, "2006年01月") }}-> March 2023
  • Simple month/day format:{{ stampToDate(item.CreatedTime, "01/02") }}-> 03/15
  • Include full time:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}-> 2023-03-15 10:30:00
  • Display the day of the week:{{ stampToDate(item.CreatedTime, "2006-01-02 Mon") }}-> 2023-03-15 Wed (In Go language formatting, 'Mon' represents Monday, etc.)

You can flexibly adjust the date display format according to the overall design style of the website and the reading habits of users to ensure that the information is clear and aesthetically pleasing.

Summary

PassstampToDateTags, AnQi CMS makes it extremely simple and efficient to display the publish date on the article list page.It not only enhances users' trust in the content, but also benefits search engine optimization (SEO), allowing the crawler to better understand the timeliness of the content.Master this little trick, and your security CMS website operation will be more refined and professional.


Common Questions (FAQ)

  1. Q:item.CreatedTimeWhy is it a long number, rather than the date format I'm familiar with?A: This is becauseitem.CreatedTimeStored is the Unix timestamp (Unix timestamp).It is an internationally recognized standard, representing the number of seconds that have passed since 00:00:00 UTC on January 1, 1970.This format is easy for machines to process, convenient for cross-platform data exchange and comparison.stampToDateTags are used to convert this machine language into a date format that is familiar to humans.

  2. Q: How do I set up to display only the year or month in the date?stampToDatethe format parameter?A: You only need to refer to the Go language's reference date2006-01-02 15:04:05Keep only the year and month parts you want to display.

    • Display only the year:{{ stampToDate(item.CreatedTime, "2006年") }}
    • Display only the month and date:{{ stampToDate(item.CreatedTime, "01月02日") }}
  3. Q: If my article has an update date, can I display the update date instead of the publish date?A: Of course you can. Anqi CMS usually also provides articles withitem.UpdatedTimeThis field records the timestamp of the last update of the article. You canitem.CreatedTimereplace