How to format the `CreatedTime` or `UpdatedTime` obtained from the `prevArchive` tag into a readable date and time?

Calendar 👁️ 64

In website operations, the time of content release or update is an important component of conveying information to users.A clear and readable date and time format, which can not only improve user experience but also enhance the professionalism and timeliness of the content.However, many content management systems usually store time in the background using a string of timestamps that are difficult to understand intuitively.prevArchiveThis tag retrieves the articleCreatedTimeorUpdatedTimeBy default, the original timestamp is returned

As an experienced website operations expert, I know how important it is to transform technical information into user-friendly displays.Today, let's delve into how to format these original timestamp data into the date and time format we are more accustomed to reading in AnQi CMS.

Understanding the original timestamp and user needs

Firstly, we need to understand why there is a situation where a series of numbers appears. Anqi CMS stores in the databaseCreatedTimeandUpdatedTimeThis time field uses the standard timestamp format, which is the number of seconds since 00:00:00 UTC on January 1, 1970 (Unix epoch). For example, you may see something similar in the template.1678886400Such numbers.Although this is very efficient and accurate for internal system processing, for website visitors, this string of numbers obviously has no meaning and cannot convey the key information of when the article was published or updated.

Our goal is toprevArchive/archiveDetailorarchiveListget tags fromitem.CreatedTimeorprev.UpdatedTimeTimestamps converted to readable formats such as 'March 15, 2023 10:30 AM' or '10:30 AM yesterday'.

The secret weapon of AnQi CMS:stampToDateTag

The AnQiCMS template engine (based on the powerful features of Go language) provides a very convenient built-in tag for this:stampToDate. This tag is used to format timestamps into the date-time string we need.

Its basic usage is:{{stampToDate(时间戳变量, "格式化字符串")}}.

The key here lies in the second parameter - 'format string'. Unlike many other languages, the Go language (as well as the AnqiCMS template engine) does not useY-m-d H:i:sInstead of such placeholders, use oneA specific reference timeBe used as a format template. This reference time is:2006年01月02日 15时04分05秒.

You are not mistaken, it is this string of numbers and words! You can use this reference time to 'mimic' it to the format you want the timestamp to be.

Let's look at a few examples:

  • If you want to display 'Year-Month-Day', for example2023-03-15Then the format string is"2006-01-02".
  • If you want to display 'Year/Year/Year/Month/Month/Day/Day', for example2023/03/15Then the format string is"2006/01/02".
  • If you want to display 'Year after year after year after year after year after year after year, every minute', for example2023-03-15 10:30Then the format string is"2006-01-02 15:04".
  • If you want to display 'Year after year after year after year after year after year after year, every minute and second', for example2023-03-15 10:30:00Then the format string is"2006-01-02 15:04:05".
  • If you want a Chinese format, for example2023年03月15日Then the format string is"2006年01月02日".

In this way, you can almost combine any date and time display format you want.

Practical application: Make time 'come alive' in the template

Now, we apply thisstampToDatelabel to a specific scenario

Scenario one: Format the time of a single article on the article detail page

Suppose you are designing a detailed article page and need to display the publication time and update time of the current article. Usually, the details of the current article are displayed through the template.archiveDirectly obtain the variable or througharchiveDetailLabel retrieval.

If you want to display the publication date as "Year-Year-Year-Month-Day-Day":

<p>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</p>

If you want to display the update time to the minute and include Chinese descriptions:

<p>最近更新:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15:04")}}</p>

This is a direct usearchive.CreatedTimeorarchive.UpdatedTimemethod, simple and efficient.

Scenario two: Formatting time in article lists or in the previous/next article

When you usearchiveListLoop through the article list, or useprevArchiveandnextArchiveTags can also be used to get the previous/next articlestampToDateTags can format time.

For example, in the display of the previous/next article:

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:<a href="{{prev.Link}}">{{prev.Title}} ({{stampToDate(prev.CreatedTime, "2006-01-02")}})</a></p>
  {% else %}
    <p>没有上一篇文章了</p>
  {% endif %}
{% endprevArchive %}

{% nextArchive next %}
  {% if next %}
    <p>下一篇:<a href="{{next.Link}}">{{next.Title}} ({{stampToDate(next.CreatedTime, "2006-01-02")}})</a></p>
  {% else %}
    <p>没有下一篇文章了</p>
  {% endif %}
{% endnextArchive %}

In the loop of the article list:

{% archiveList archives with type="list" limit="10" %}
  {% for item in archives %}
    <div>
      <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
      <p>发布于:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
      <p>{{item.Description}}</p>
    </div>
  {% endfor %}
{% endarchiveList %}

You can see, whether it is a single article detail or each item in the list,stampToDatethe label can flexibly convert the timestamp into the readable format we want.

optimize and **practice

In actual operation, date and time formatting is not just about technical implementation, but also about user perception.

  • Maintain consistency: The format of publication time and update time should be as consistent as possible throughout the website to avoid confusion for users.
  • Consider the contextFor news information websites, the publication time may need to be precise to the minute; while for long technical articles, it is sufficient to display to the day.Choose the appropriate format based on content type and user habits.
  • Utilize the flexibility of the Go language: The time reference format of Go language may look unique at first glance, but it is very flexible. You cantag-stampToDate.mdfind more detailed formatting examples in the documentation, and explore more possibilities.

Summary

Provided by Anqi CMSstampToDateLabel, we can easily convert the timestamp data from the backend into a user-friendly and easily understandable date and time format. Whether you are displaying the publish time of a single article or in an article

Related articles

Can the `prevArchive` tag retrieve the `Description` (summary) field of the previous document?

Sure, as an experienced website operations expert, I am happy to delve into the usage of the `prevArchive` tag in AnQi CMS and answer questions about obtaining the Description field of the document. --- ## Unlock Anqi CMS `prevArchive` tag: Easily get the summary of the previous document In daily website operations, we often need to provide users with a smooth reading experience and guide them to discover more related content. This includes

2025-11-07

The document for the previous archive tag is determined by what logic or sorting rules?

## Unveiling the Anqi CMS `prevArchive` tag: The intelligent sorting logic of the "Previous" document As an experienced website operations expert, I know that behind each CMS tag lies the potential to enhance user experience and SEO efficiency.AnQiCMS (AnQiCMS) is known for its efficiency and flexibility, and its template tag system is an extremely powerful tool for content operations.Today, let's delve into a seemingly simple but actually cleverly logical tag: `prevArchive`, and how it intelligently determines what we mean by 'previous document'

2025-11-07

How to add custom CSS styles or HTML attributes to the link generated by the `prevArchive` tag?

As an experienced website operations expert, I am well aware of the importance of meticulous template customization in content management systems for enhancing user experience and the overall style of the website.AnQiCMS (AnQiCMS) provides us with great flexibility with its efficient architecture based on the Go language and a Django-like template engine.Today, let's delve into a common customization requirement: how to add custom CSS styles or HTML attributes to the link generated by the `prevArchive` tag for the 'previous article'.

2025-11-07

`prevArchive` label outside the article detail page (such as the list page) whether it can still effectively obtain the previous document?

## Deep Dive into the `prevArchive` tag of Anqi CMS: The Mystery of the 'Previous' on List Pages As an experienced website operations expert, I am well aware that in daily content management, every tag and every function contains the potential to improve user experience and operational efficiency.AnQiCMS, with its simple and efficient features and SEO-friendly characteristics, has become a powerful assistant for many small and medium-sized enterprises and self-media operators.

2025-11-07

Does the `prevArchive` tag support getting custom field content from the backend of the previous document?

As an experienced website operation expert, I have a deep understanding of AnQiCMS (AnQiCMS) template tags and content operation strategies, and I am glad to give you a detailed explanation of the function of the `prevArchive` tag and discuss how to cleverly obtain the custom field content of the previous document. --- ### In-depth Analysis of AnQiCMS `prevArchive` Tag and Custom Field Retrieval Strategy In AnQiCMS template development, `prevArchive` and `nextArchive`

2025-11-07

How to avoid the `prevArchive` tag from outputting an empty value or causing a template error when there is no previous document?

As an experienced website operations expert, I know that the robustness of templates in content management systems is crucial for the stable operation of the website and the user experience.AnQiCMS with its efficient and flexible template system has won the favor of many users, but even an excellent system needs to master some skills in practical application to fully exert its power.Today, let's delve deeply into a common but often overlooked issue in AnQiCMS template development: how to avoid the `prevArchive` tag from outputting an empty value or causing the template to error when there is no previous document

2025-11-07

How to effectively use the `prevArchive` tag to enhance the internal link structure and user navigation experience of a website?

## Unlock AnQi CMS `prevArchive` tag: A comprehensive guide to effectively optimizing internal links and user navigation experience In today's increasingly user experience and search engine optimization-focused era, the internal link structure and navigation fluidity of a website are particularly important.As an experienced website operations expert, I am well aware of the powerful potential of AnQiCMS (AnQiCMS) in content management and SEO.

2025-11-07

Can the `prevArchive` tag retrieve the `Keywords` (keywords) field of the previous document?

As an experienced website operations expert, I know that a detailed understanding of each tag (Tag) function in a content management system can bring a qualitative leap in the operational efficiency and SEO performance of the website.Today, let's delve deeply into a very practical template tag in AnQiCMS (`prevArchive`), focusing on whether it can retrieve the `Keywords` field of the previous document.

2025-11-07