How to format a timestamp into a readable format

Calendar 👁️ 66

Let time speak: How to format a timestamp into a readable date in Anqi CMS

When operating a website, the timeliness of content is often a point of great concern for users.Whether it is the publication time of an article, the update time of product information, or the specific moment of user comments, a clear and intuitive date and time can greatly enhance user experience, and even have a positive impact on search engine optimization (SEO).1609470335This is what is called a timestamp, it is machine language, and it is not intuitive for ordinary users.

AnQi CMS is an efficient and easy-to-use content management system that fully considers the actual needs of users. It provides us with a very convenient and powerful tool to solve this problem, that isstampToDateThe label makes it easy to convert those cold timestamps into a date and time format that everyone can understand, with a touch of warmth.

Get to knowstampToDateTag

stampToDateThe tag is a built-in function in Anqi CMS template engine, specifically used for timestamp formatting. Its basic usage is very intuitive:

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

Here时间戳It is usually a 10-digit Unix timestamp (representing the number of seconds from 1970-01-01 00:00:00 UTC to the present), and格式The specific style you want the date and time to display.

The key is in how to define this 'format'.The template engine of AnQi CMS is based on Go language, therefore it follows the unique time formatting rules of Go language.YYYY-MM-DDInstead of such placeholders, use oneA specific reference time:2006-01-02 15:04:05. You need to map the date and time format you want to this reference time. Sounds a bit abstract? Don't worry, we'll understand through some examples.

List of common formatting modes

MasterstampToDateThe key is to understand this reference time in Go language. The following table will show some common date and time display requirements, as well as the corresponding format strings:

Display requirements Format string (Go language reference time) Sample output (assuming timestamp is)1609470335Corresponding to January 1, 2021, 11:05:35
Date
Year-Month-Day (e.g., 2021-01-01) 2006-01-02 2021-01-01
Year Month Day (e.g., 2021 January 01) 2006年01月02日 2021年01月01日
Month/Day/Year (e.g., 01/01/2021) 01/02/2006 01/01/2021
Time
Time:Hour:Minute:Second (e.g., 11:05:35) 15:04:05 11:05:35
Time:Hour:Minute (e.g., 11:05) 15:04 11:05
Complete Date and Time
Year-Month-Day Hour:Minute:Second 2006-01-02 15:04:05 2021-01-01 11:05:35
Year-Month-Day Hour:Minute:Second 2006年01月02日 15时04分05秒 2021年01月01日 11时05分35秒
Day, Month Date Year Time:Minutes:Seconds Mon, 02 Jan 2006 15:04:05 Fri, 01 Jan 2021 11:05:35

With this reference table, you can flexibly combine almost any date and time format you want

Apply in your Anqi CMS template

Now, let's see how to use it in an actual Anqi CMS template filestampToDatetags. Whether it's an article detail (archiveDetail) or an article list (archiveList) Or comment list (commentList) Many time fields such asCreatedTime/UpdatedTime) are provided in the form of 10-digit timestamps.

Example one: Display the publish date on the article detail page

Assuming you want to display the publication date of the article on the ({模型table}/detail.htmlor a custom article template) page. The article object'sCreatedTimefield stores the publication timestamp.

{# 默认获取当前页面的文档对象 archive #}
<div>
    发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}
</div>
<div>
    更新时间:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}
</div>

Example two: Display the publication time of each article on the article list page

When you are on an article list page ({模型table}/list.htmlor a custom list template) and loop through multiple articles, you can useforin the loopitem.CreatedTime.

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>
            发布于:<span>{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>
            浏览量:<span>{{item.Views}}</span>
        </p>
        <p>{{item.Description}}</p>
    </li>
    {% endfor %}
{% endarchiveList %}

Example 3: Display time in comments or messages

When a user submits a comment or message, the system will also record the timestamp. In the comment list (comment/list.html) you can format the time like this:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
        <strong>{{item.UserName}}</strong> 于 <span>{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> 评论道:
        <p>{{item.Content}}</p>
    </div>
    {% endfor %}
{% endcommentList %}

Tip

  • Try flexibly:The time formatting in Go language is very flexible, you can try different combinations to achieve the display effect you want. For example, you can only display the year2006or only display the month01.
  • Field Name:Please note,CreatedTimeandUpdatedTimeThese fields themselves store timestamp values, which can be directly passed tostampToDateFunction.
  • Background timed release:Safe CMS supports the scheduled publishing of content.If you set the publish time to a future date, the front-end page usually will not display the content until the specified time arrives.CreatedTimeand will be correctly parsed and displayed.

BystampToDateLabel, you can easily present time information on the website more clearly and friendly. This not only improves the user reading experience, but also enhances the content of your website

Related articles

How to safely output user-generated content in AnQiCMS templates to prevent XSS attacks from affecting the display of the page?

The website content operation is in progress, user-generated content (UGC) is undoubtedly a valuable resource for enhancing the vitality and interactivity of the website.Whether it is comments, messages, forum posts, or articles edited with a rich text editor, all of these greatly enrich the information ecology of the website.However, potential security risks come with UGC, the most common and severe of which is cross-site scripting (XSS) attacks.If not prevented, malicious script code may be executed in the user's browser, stealing user data, and tampering with the page

2025-11-08

How to implement the template inheritance function of AnQiCMS, so that the content block display of master page and sub page can be overlaid and customized?

How template inheritance in AnQiCMS allows the parent page and child page to perform their respective functions?In website content management, maintaining consistent page style while being able to flexibly modify local content is a goal pursued by many operators.AnQiCMS uses a syntax similar to the Django template engine, providing us with an elegant solution, that is, the powerful template inheritance feature.With this feature, we can cleverly design master pages and subpages, making the website structure both unified and highly customizable.

2025-11-08

How to use the `macro` macro function in AnQiCMS templates to define reusable content display segments to improve efficiency?

In AnQi CMS, efficiently managing website content and front-end display is the key to daily operations.When faced with some repetitive page elements, such as article list items, product cards, or buttons with specific styles, if you rewrite the code every time, it not only takes time and effort, but is also prone to errors, and future modifications and maintenance will become extremely complex.

2025-11-08

How to use the `include` tag in AnQiCMS templates, efficiently reuse and display common page header, footer and other modules?

When building a website, we often encounter such a scenario: the website header (Header), footer (Footer), sidebar (Sidebar), and navigation menu modules, almost appear on every page.These modules are not only similar in content, but also their structure and style need to be maintained consistently.If you write the same code for each page, it is not only inefficient, but also a maintenance nightmare if you need to make any changes, as you would have to adjust all pages one by one.Luckyly, AnQiCMS (AnQiCMS) understands the importance of template reuse

2025-11-08

How to filter and display content on the article list page based on specific conditions (such as recommended attributes)?

In our website content operation, the article list page often plays an important display role.We do not want to simply arrange the content mechanically according to the time of publication, but we hope to be able to flexibly filter and display articles based on certain conditions, such as the importance of the content, popularity, or whether it is recommended.AnQiCMS (AnQiCMS) was designed with these needs in mind from the outset, providing us with a simple yet powerful content filtering mechanism.

2025-11-08

What ways does Anqi CMS support to adjust the overall content display mode of the website (adaptive, code adaptation, PC+mobile independent site)?

AnQi CMS: Three Flexible Options for Customizing Website Content Display Mode How to present the effect of website content on different devices is an important issue that every webmaster needs to consider when building and operating a website.Anqi CMS understands this point and therefore provides a variety of flexible content display modes, allowing you to choose the most suitable way to build your website according to your actual needs.No matter if you are pursuing a simple and efficient experience or need an ultimate customization experience, AnQi CMS can provide strong support.Next, we will delve into the three website content display modes supported by Anqi CMS

2025-11-08

How to utilize the flexible content model of Anqi CMS to define display fields for different types of content?

Today, with the increasing richness and diversity of digital content, the flexibility of a website content management system (CMS) is crucial.AnQiCMS (AnQiCMS) is well aware of this, one of its core advantages is that it provides a powerful "flexible content model" feature, allowing users to customize exclusive display fields for various content according to different business needs.This greatly enhances the adaptability of the website, making content operation more efficient and personalized.

2025-11-08

How does AnQi CMS ensure the correct switching and display of different language content on a multilingual website?

Today, with the deepening of globalization, website content can reach different language user groups and has become a key factor for enterprises to expand the market and enhance brand influence.For users who operate multilingual websites, one of the most concerning issues is how to ensure the correct switching and display of different language content?AnQiCMS (AnQiCMS) provides a clear and efficient solution in this regard, let's take a look at how it is implemented.

2025-11-08