How to format the date within the `prevArchive` tag by combining the `stampToDate` filter?

Calendar 👁️ 62

As an experienced website operation expert, I have accumulated rich experience in the practice of AnQiCMS.I deeply understand that the way content is presented is crucial for user experience, even something as seemingly trivial as the date format may affect the user's reading experience and the professionalism of the website.prevArchiveCombine within the tagstampToDateThe filter beautifully formats time.


Elegantly present the time of the previous article in AnQiCMS

When building website content, we often need to provide users with convenient navigation functions, such as 'Previous article' or 'Next article' links. AnQiCMS provides this for users.prevArchiveandnextArchiveSuch convenient tags, they can help us easily get information about adjacent articles.However, when we try to display the time data contained in these tags directly, we may find that they appear in the form of raw timestamps, which is not intuitive for ordinary users.stampToDateThe filter comes into play here, it can turn the cold timestamp into an easily understandable date and time format.

UnderstandprevArchiveTag

First, let's briefly review.prevArchiveThe function of the tag. When you are on the article detail page and want to display a link and a brief introduction to the previous article, you can use this tag. Its basic structure is usually as follows:

{% prevArchive prev %}
  {% if prev %}
    <a href="{{prev.Link}}">{{prev.Title}}</a>
  {% else %}
    <span>没有了</span>
  {% endif %}
{% endprevArchive %}

Here, prevIt is the variable name defined for the data of the previous article. Throughprev, we can access various fields of the previous article, such asprev.Linkget the article link,prev.TitleGet the article title. The time information is usually stored inprev.CreatedTime(creation time) andprev.UpdatedTimethe (update time) field. The values of these fields are standard Unix timestamps, such as1609470335Such a sequence of numbers. It is obvious that users cannot understand these numbers directly.

IntroductionstampToDateFilter: The magician of time.

To convert these timestamps into a user-friendly format, AnQiCMS providesstampToDateThis powerful filter. Its function is to receive a timestamp and a format string as parameters, and then output the formatted date and time.

stampToDateThe syntax is very intuitive:{{stampToDate(时间戳, "格式")}}. The 'timestamp' here is the number we obtained fromprev.CreatedTimeorprev.UpdatedTimeand the 'format' string is the key.

The template engine of AnQiCMS is developed based on Go language, therefore, its time formatting follows the Go language'stime.FormatThe rule of 'reference time' used by the function. This reference time is a fixed value:2006-01-02 15:04:05.999999999 -0700 MST.You do not need to remember this long string of numbers and letters, just know that when you want to represent year, month, day, hour, minute, second information, you use the corresponding part of this reference time to 'occupy'.

  • YearUse2006to represent a four-digit year (such as 2023).
  • MonthUse01To represent two-digit months (e.g., 09), useJanTo represent the English abbreviation of months (e.g., Sep), useJanuaryTo represent the English full month name (e.g., September).
  • DateUse02To represent a two-digit date (e.g., 15), use_2To represent a date without leading zeros (e.g., 5).
  • hoursUse15To represent 24-hour time (e.g., 13), use03To represent 12-hour time (e.g., 01).
  • minuteUse04represents two digits of minutes (such as 08).
  • secondsUse05represents two digits of seconds (such as 30).

After mastering this basic principle, we can flexibly combine various date and time formats.

InprevArchiveChinese formatted time: practical exercise

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

Suppose we want to display the title of the previous article at the same time as its publication date, formatted as "YYYY年MM月DD日".

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:
      <a href="{{prev.Link}}">{{prev.Title}}</a>
      <!-- 格式化发布时间为“年年年年-月月-日日” -->
      <small>发布于:{{stampToDate(prev.CreatedTime, "2006-01-02")}}</small>
    </p>
  {% else %}
    <p>没有上一篇文章了</p>
  {% endif %}
{% endprevArchive %}

In this code,{{stampToDate(prev.CreatedTime, "2006-01-02")}}Willprev.CreatedTimeThis timestamp is converted to2023-09-15This format.

If you need to display the specific publishing time, such as to the hour and minute, you can adjust the format string like this:

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:
      <a href="{{prev.Link}}">{{prev.Title}}</a>
      <!-- 格式化发布时间为“年年年年-月月-日日 时时:分分” -->
      <small>发布于:{{stampToDate(prev.CreatedTime, "2006-01-02 15:04")}}</small>
    </p>
  {% else %}
    <p>没有上一篇文章了</p>
  {% endif %}
{% endprevArchive %}

This may output发布于:2023-09-15 10:30.

Or if you need a more Chinese context expression, for example, the format string can be written as:

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:
      <a href="{{prev.Link}}">{{prev.Title}}</a>
      <!-- 格式化发布时间为“年年年年年月月月日日日” -->
      <small>发布于:{{stampToDate(prev.CreatedTime, "2006年01月02日")}}</small>
    </p>
  {% else %}
    <p>没有上一篇文章了</p>
  {% endif %}
{% endprevArchive %}

By flexibly using the reference time format of Go language, you can easily achieve any date and time display effect you need, whether it isYYYY/MM/DD/MM-DD HH:mmor星期一, 02 Jan 2006,stampToDateit can help you achieve it.

AnQiCMS providesstampToDateThis powerful filter allows template developers to easily format time information in dynamic content, greatly enhancing the flexibility and user experience of content presentation.Make good use of these tools, your website can not only provide rich content, but also present every detail in a professional and user-friendly manner.


Frequently Asked Questions (FAQ)

Q1: Why do I need to usestampToDatea filter to format the time instead of displaying it directly{{prev.CreatedTime}}?

directly displaying{{prev.CreatedTime}}will output a pure numeric timestamp (for example:1609470335This format is hard to understand for ordinary website visitors.stampToDateThe role of the filter is to convert this machine-readable timestamp into a human-readable date and time string, for example2023年09月15日or10:30 AMThus greatly enhancing the user experience and readability of the content.

Q2:stampToDateWhat are the special requirements for the 'format' string in the filter? How can I remember it?

stampToDateThe format string follows the Go language'stime.Formatfunction specification, which uses a specific reference time2006-01-02 15:04:05Define the parts of a date and time.You do not need to remember the date represented by this reference time itself, just remember the meaning of each number (2006 represents the year, 01 represents the month, 02 represents the day, etc.).If you need a more complex format, refer to the AnQiCMS template tag document, which will list all options and examples of Go language time formatting in detail."2006-01-02"or"2006-01-02 15:04"Can meet most needs already.

Q3:stampToDateThe filter can only be used withprevArchiveTags, can you?

No, it's not.stampToDateIs a universal time formatting filter. As long as you can get any time data in the form of a Unix timestamp (for example, fromarchiveDetailobtainedarchive.CreatedTime, or fromarchiveListin the loopitem.UpdatedTime), you can use it asstampToDateThe first parameter, for formatting output. It can be used in any scenario of AnQiCMS template where timestamp needs to be converted to a specific datetime format, which gives you great flexibility to unify the style of time display on the website.

Related articles

Does the `prevArchive` tag automatically escape HTML in the document title and other text content?

AnQi CMS is an efficient and secure management system, its template engine provides great flexibility in content display, and it also has comprehensive security mechanisms built-in.For the question you raised, will the text content obtained by the `prevArchive` tag automatically perform HTML escaping?This question involves the core security strategy of template rendering, which is also a key point that we need to clearly understand in website operations.From a professional website operation perspective, AnQiCMS's template engine handles the text content from the database and outputs it to the web page

2025-11-07

Why does the `prevArchive` tag not provide filtering parameters such as `categoryId` or `moduleId`?

As an experienced website operation expert, I fully understand how important it is to explore the details of tag functions in the process of content management and template development.AnQiCMS is known for its efficiency and flexibility, but some of the label design logic does indeed raise users' questions during use.Today, let's delve into why the `prevArchive` tag does not provide `categoryId` or `moduleId` filtering parameters?This topic. First

2025-11-07

Does the operation of the `prevArchive` tag have a significant impact on server resources and page loading speed?

Website performance is the foundation of modern digital marketing success.Users expect content to be displayed instantly, and search engines also prefer websites that load quickly.Therefore, when we build pages in content management systems like AnQiCMS, the running efficiency of each template tag is worth paying attention to.Today, let's delve into whether the operation of the `prevArchive` tag has a significant impact on server resources and page loading speed?This is a question that many operators and developers are concerned about.

2025-11-07

How to handle the adaptive image size issue when displaying images using the `prevArchive` tag?

As an experienced website operations expert, I know well how to elegantly handle image display in a content management system, especially in terms of responsive design and performance optimization, which is the key to improving user experience and SEO performance.AnQiCMS (AnQiCMS) with its powerful functions and flexible template system, provides us with many solutions.Today, let's delve into a common and practical topic: how to handle image size adaptation when displaying images using the `prevArchive` tag.

2025-11-07

Where is the `prevArchive` tag most appropriately placed in template development?

As an experienced website operations expert, through my practice with AnQiCMS, I know that the flexible application of template tags is the key to improving website user experience and SEO performance.Today, let's delve deeply into a seemingly simple yet highly practical tag —— `prevArchive`, and its most suitable placement in template development.### Understanding the Core Role of `prevArchive` In AnQiCMS, `archive` refers to all types of content, whether articles, products, cases, or other custom model content

2025-11-07

How AnQiCMS helps small and medium-sized enterprises and content operations teams improve efficiency?

In today's highly competitive digital environment, small and medium-sized enterprises and content operations teams are seeking smarter and more efficient tools to meet the rapidly changing market needs and maximize the value of content.Traditional CMS systems are often cumbersome and complex, or lack sufficient functionality, making it difficult to truly meet the needs of efficient operation.AnQiCMS is a corporate-level content management system dedicated to solving these pain points, it brings real efficiency improvement to users through a series of innovative functions and technical advantages.

2025-11-07

What are the technical advantages of AnQiCMS in handling high concurrency access?

## Tackling the Digital Deluge: AnQiCMS's Technical Proficiency in High-Concurrent Access In today's ever-changing digital world, websites act as a bridge of communication between enterprises and users.Especially for small and medium-sized enterprises and content operation teams, how to ensure that the website remains smooth and stable during high traffic and high concurrency access is a key link to the success of website operation.

2025-11-07

How to create and manage multiple independent websites in AnQiCMS?

As an experienced website operations expert, I am well aware of the importance of efficiently managing multiple websites in an increasingly complex online environment for corporate brand building and content marketing.AnQiCMS (AnQiCMS) boasts excellent performance and rich features, making it an ideal choice for many small and medium-sized enterprises and content operation teams.Today, let's delve into how Anqi CMS can help you easily create and manage multiple independent websites.

2025-11-07