As an experienced website operations expert, I fully understand the importance of flexible data display and user experience.The AnQi CMS provides many conveniences for content operation with its powerful customization capabilities.Today, let's delve into a very practical skill in daily content presentation: how totagDataListIn, beautifully format the creation time of the associated document.

Timestamp in AnQi CMS template: why do you need to format it?

In Anqi CMS, whether it is articles, products, or other content models, their creation time (CreatedTimeAnd update time(UpdatedTimeDates such as these are usually stored in the database in the form of Unix timestamps (Timestamp). This format is efficient and easy to handle for computers, but a string like1678886400Such numbers are meaningless.

As website operators, our goal is to provide clear and readable information.Convert these original timestamps to user-friendly date formats such as “March 15, 2023”, “10:30 AM yesterday”, or “Mar 15, 2023”, which can not only enhance the professionalism of the website but also improve the reading experience of users.

tagDataListA powerful tool for linking documents.

In the template design of AnQi CMS,tagDataListTags are a very practical tool, allowing us to retrieve and display lists of associated documents based on specific tags (Tag).For example, we may need to display all articles with the 'SEO optimization' tag on a page, or list all products related to 'new product launch'.

tagDataListWorks in a very intuitive way. It returns an array containing document objects, and we can access them throughforLoop to traverse these documents. Each document object (usually nameditem) contains various properties of the document, such asId/Title/Link/Description, of course, including the one we are concerned aboutCreatedTime.

Basic usage is as follows:

{% tagDataList archives with tagId="1" %}
    {% for item in archives %}
        <p>{{ item.Title }} - 创建时间:{{ item.CreatedTime }}</p>
    {% endfor %}
{% endtagDataList %}

If not processed{{ item.CreatedTime }}The output here is that cold, cold Unix timestamp.

stampToDate: The magic wand of time formatting

To transform these timestamps into a readable date format, Anqi CMS provides a very convenient built-in tag function:stampToDateThis function is designed specifically for timestamp formatting, and its usage is also very simple and clear.

stampToDateThe function requires two parameters:

  1. Timestamp (Timestamp)This is the original Unix timestamp you need to format. IntagDataListthe loop, it is usuallyitem.CreatedTime.
  2. Format String (Format String)This is a string defining the output date format. It should be noted that AnQi CMS is developed based on Go language, so this format string follows the Go language'slayoutStandard, that is, using oneFixed reference timeto define the format, rather than using placeholders like PHP or JavaScript (such asY-m-d)

The reference time in Go language is:2006-01-02 15:04:05This means, if you want to display the year, you write2006; to display the month, you write01; to display the date, you write02and so on.

Practice exercise: to transformtagDataListthe creation time into a magnificent transformation

Now, let's taketagDataListandstampToDateCombine them and see how to apply them in actual templates.

Assuming we have a TagID of1label, we need to list all associated document titles and their creation dates, displayed in the format of "Year-Month-Day Time:Minute":

{% tagDataList archives with tagId="1" %}
    {% for item in archives %}
        <div class="document-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p>
                发布于:
                {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}
                <span>浏览量:{{ item.Views }}</span>
            </p>
            <p>{{ item.Description }}</p>
        </div>
    {% else %}
        <p>当前标签下暂无关联文档。</p>
    {% endfor %}
{% endtagDataList %}

In the code above,item.CreatedTimePassed as the first parameterstampToDatewhile"2006-01-02 15:04"It acts as a format string, telling the system the date format we want to output.

Output example:

发布于:2023-03-15 10:30

Custom format, do as you wish

The date formatting method of the Go language may seem special at first glance, but once you master its principles, you can achieve extremely flexible custom formats. Here are some common formatting examples that can help you generalize from one to another:

  • Show only the year and month: {{ stampToDate(item.CreatedTime, "2006年01月") }}
    • output:2023年03月
  • Show English month and date: {{ stampToDate(item.CreatedTime, "Jan 02, 2006") }}
    • output:Mar 15, 2023
  • Show the full name of the weekday: {{ stampToDate(item.CreatedTime, "Monday, 2006年01月02日") }}
    • output:Wednesday, 2023年03月15日
  • Show AM/PM: {{ stampToDate(item.CreatedTime, "2006-01-02 03:04 PM") }}
    • output:2023-03-15 10:30 AM(NotePMCapitalize,03:04For 12-hour format)
  • Show short date: {{ stampToDate(item.CreatedTime, "01/02/06") }}
    • output:03/15/23

By these examples, you can freely combine the most appropriate date format based on the website's UI/UX design and the reading habits of the target audience.

Insights from content operation.

Precise and user-friendly time display is an indispensable part of high-quality content operation.It is not just a technical implementation, but also has a direct impact on the users' trust in website information and their dwell time.Whether it is news updates, blog articles, or product releases, clear publication or update times can help users quickly obtain key information and enhance the overall browsing experience.Provided by AnQi CMSstampToDateFunction, making everything simple and efficient.

Summary

MastertagDataListandstampToDateThe combination of use, is one of the basic skills for security CMS content operators.By explaining and providing examples in this article, I believe you can easily convert the original timestamp into various elegant and readable date formats in the AnQiCMS template, thereby adding luster to your website content.


Frequently Asked Questions (FAQ)

1.stampToDateWhy is the format string in the function so special, not common?Y-m-d?

This is because Anqie CMS is developed based on Go language, and its template engine follows the standard of Go language when processing date formats. The date formatting of Go language is based on a "reference time", namely2006-01-02 15:04:05.999999999 -0700 MSTEach number or letter you write in the format string corresponds to a specific component of this reference time (for example,2006represents the year,01Represents the month). Just write according to the reference time, and the system will automatically identify and output the corresponding time information. Although it may be a bit strange at first contact, it is very precise and flexible.

2. BesidestagDataListCan I also use it in other tags?stampToDateCome format the time?

Of course, I can. All returned documents in AnqiCMS (such asarchiveList/archiveDetail), categories (such ascategoryList/categoryDetail), or single pages (such aspageList/pageDetail) tags, if their internal data structure containsCreatedTimeorUpdatedTimeThis timestamp field can be usedstampToDateformatted by the function. The usage is the same as intagDataList, just replaceitem.CreatedTimewith the timestamp field corresponding to the label loop variable.

3. If I find that the creation time in the template is displayed directly as a long string of numbers (like1678886400), instead of a formatted date, how should I troubleshoot the issue?

This usually meansstampToDatethe function does not