How to use the `stampToDate` tag to format a database timestamp into a readable date?

Calendar 👁️ 70

In website operation, we often encounter such situations: the time information stored in the database, such as the publication time of articles, the creation time of products, etc., is often in the form of a string of numbers (timestamp).This sequence of numbers is clear to machines, but readability is far worse for us users.Imagine, an article displayed next to the date "1678886400" instead of "2023 March 15", doesn't it greatly reduce the experience?AnQi CMS knows this and has provided a very practical template tag -stampToDateHelp us easily format these original timestamps into beautiful and readable dates and times.

Why do we need to format timestamps?

The timestamp stored in the database is typically an integer representing the number of seconds or milliseconds since January 1, 1970, 00:00:00 UTC (Unix epoch).This unified storage method facilitates data processing and cross-time zone calculations, but displaying these numbers directly on the front-end page may confuse users and affect reading experience.Convert them to the format

Get to knowstampToDateTags and their usage

Of Security CMSstampToDateThe tag is designed to be very concise and efficient, it mainly requires two key pieces of information to complete the timestamp formatting:

  1. Original timestamp: This is the string of numbers you retrieved from the database. In AnQi CMS, this is usually a10 digitsinteger representing a second-level timestamp. For example,1609470335.
  2. Format stringThis is the template for defining how you want the date and time to be displayed. It may be used by some other systems.YYYY-MM-DDThis placeholder is different, Anqi CMS (developed based on Go language) uses the Go language's unique date and time formatting method. It uses a fixedreference date2006-01-02 15:04:05As a formatted "sample".

Please note, this reference date itselfwill notbe actually output. You need to match the components of the date and time you want with the reference date in the following.specific numberto write the format string. For example:

  • If you want to display a four-digit year, write2006.
  • If you want to display a two-digit month, write01.
  • If you want to display a two-digit date, write02.
  • If you want to display 24-hour time, write15.
  • If you want to display a two-digit minute number, write04.
  • If you want to display seconds with two digits, write05.
  • If you want to display the hour in 12-hour format, write3,and cooperatePMorAM.

stampToDateThe basic syntax structure of the tag is:

{{ stampToDate(您的时间戳变量或数字, "您的格式字符串") }}

Actual operation and example

Let's look at some specific examples to see.stampToDateHow tags make timestamps 'alive'. Suppose we have an article objectitem, its published timestamp field isCreatedTime(with a value of1609470335as of January 1, 2021, 12:25:35 PM.

  1. Only show "Year-Month-Day":

    If you want to display a concise date in the article list, for example “2021-01-01”:“

    <p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
    

    The result will be:2021-01-01

  2. Display detailed “year-month-day hour:minute:second”:“

    If you need to display time information accurate to seconds, you can add references to hours, minutes, and seconds in the format string:

    <p>更新时间:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04:05") }}</p>
    

    The result will be:2021-01-01 12:25:35

  3. Customize Chinese or other delimiter formats:

    stampToDateThe strength lies in its flexibility. You can freely combine text and reference date numbers to create any format you want:

    <p>文章发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
    

    output:2021年01月01日

    <p>日期:{{ stampToDate(item.CreatedTime, "01/02/2006") }}</p>
    

    output:01/01/2021

    You can even use 12-hour clock and AM/PM markings:

    <p>具体时间:{{ stampToDate(item.CreatedTime, "3:04 PM") }}</p>
    

    output:12:25 PM(If it is afternoon, display PM, and in the morning, display AM)

  4. Common usage on the article detail page:

    On the article detail page, you usually have a current article object, for examplearchive. You can call its time field like this:

    <p>发表于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</p>
    <p>最后编辑:{{ stampToDate(archive.UpdatedTime, "2006-01-02 星期一") }}</p>
    

    The formatting rules of Go language also support more advanced usage, such as displaying the day of the week, month, etc.

Related articles

How to flexibly control the conditional display and loop iteration of logic tags such as `if` and `for` in the template?

In the world of AnQi CMS templates, we often need to display content based on different conditions or repeat a series of data, at this point, the `if` and `for` logical tags become particularly important.They are the foundation for building dynamic content and implementing flexible layouts in templates, allowing us to precisely control the presentation of every piece of information on the web page.

2025-11-08

How to use the `pagination` tag to generate a beautiful pagination navigation for articles or product lists?

In website operation, as the content volume continues to grow, whether it is articles, products, or other lists, how to efficiently organize and display these contents while ensuring a smooth user browsing experience and a friendly search engine is a problem that needs to be carefully considered.Page navigation is the key tool to solve this problem.AnQiCMS provides a powerful and flexible `pagination` tag that allows you to easily create beautiful and functional pagination navigation for various lists.

2025-11-08

How to use the `linkList` tag to display the list of friends' links on the website?

During the operation of our website, friendship links not only help to increase the amount of traffic, but are also an indispensable part of SEO optimization.A healthy link ecosystem that can effectively pass on weight and enhance the authority of the website.In AnqiCMS, managing and displaying friend links is very convenient, mainly due to its built-in `linkList` tag.This article will introduce in detail how to use the `linkList` tag to display the friend link list on your AnqiCMS website.

2025-11-08

How to quickly generate a customizable website guestbook form with the `guestbook` tag?

In website operation, interacting with visitors and collecting feedback is an important link to improve user experience and obtain valuable information, and the comment form is the core tool to achieve this goal.AnQiCMS (AnQiCMS) fully understands its importance and provides the `guestbook` tag for users, allowing you to easily and flexibly build a powerful and highly customizable website guestbook form.

2025-11-08

How `include`, `extends`, and `macro` tags enhance the reusability and maintainability of template code?

## Improving AnQi CMS template efficiency and maintainability: the clever use of `include`, `extends`, and `macro` In the daily content operation of AnQi CMS, we often need to create and manage a large number of pages.How to ensure that these pages maintain consistency while also being developed and maintained efficiently is a challenge that every operator has to face.

2025-11-08

How do `filters` (such as `truncatechars`, `safe`) format or sanitize the output content?

In the daily operation of AnQiCMS, the way content is presented and the security are the key factors that determine the professionalism and user experience of the website.AnQiCMS is a powerful template engine that draws many advantages from the Django template engine syntax, among which the 'filters' are the behind-the-scenes masters of content formatting and security processing.These filters can help us easily process output content, whether it is to simplify text, convert formats, or ensure the safety of the content, it becomes accessible.### Transformer: Content Output Transformer In simple terms

2025-11-08

How to render Markdown editor content correctly on the front-end page?

In AnQiCMS, Markdown is an efficient and widely popular content creation method.It uses a concise markup syntax, allowing creators to focus on the content itself without being troubled by complex layout tools.However, to present these concise Markdown texts to the user in the browser, it requires the collaborative processing of the AnQiCMS backend and the frontend template.Understanding this process can help you better utilize the content management capabilities of AnQiCMS, ensuring that your content is displayed in **status**.### Enable

2025-11-08

How to correctly display mathematical formulas and flowcharts in Markdown content?

With the popularity of the built-in Markdown editor of AnQiCMS, many users hope to be able to display information more flexibly in their website content, especially for technical documents, tutorials, or data analysis, the presentation of mathematical formulas and flowcharts has become particularly important.AnQiCMS integrates external libraries to bring powerful extension capabilities to Markdown content, making it easy and convenient to display professional content.

2025-11-08