How to use `stampToDate` to format a timestamp into a time format that includes AM/PM indicators?

Calendar 👁️ 76

As an experienced website operations expert, I am well aware that the way website content is presented is crucial for user experience and information transmission efficiency.Especially when dealing with basic information such as time, formatting it in a way that is familiar to the user (for example, including the 12-hour AM/PM format) will greatly enhance the professionalism and user-friendliness of the website.AnQiCMS (AnQiCMS) is a powerful and detail-oriented content management system that naturally provides flexible time formatting tools.Today, let's delve into how to make use of AnQiCMS'sstampToDateLabel, turn the timestamp into a professional time display that includes AM/PM indicators.


How to format a timestamp into a time format that includes AM/PM indicators?

In the template world of AnQi CMS, the precise and beautiful presentation of time is a key factor in improving user experience.Whether it is the publication time of the article, the update date of the product, or the submission time of the comment, a clear and readable date format always makes the user feel comfortable.When we need to convert the original timestamp (a string of numbers) into a date and time that people are accustomed to reading, especially when it includes indicators such as 'AM' or 'PM', AnQiCMS provides a very convenientstampToDate.

Get to knowstampToDate: The magic wand of time formatting

stampToDateIs an AnQiCMS template engine core timestamp formatting tag.The function of it is to convert a Unix timestamp (an integer representing the number of seconds from 1970-01-01 00:00:00 UTC to the present) into a human-readable date and time string.The charm of this tag lies in its extremely high flexibility, which can format timestamps into almost any shape you want according to your needs.

Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}

The "timestamp" typically comes from the content data (such as the article'sCreatedTimeorUpdatedTime), and the "format" is the key to defining the output appearance.

Master Golang time formatting: the key of AM/PM

AnQiCMS is developed based on Go language at its core, therefore its time formatting follows the unique specifications of Go language. Go language does not useY-m-d H:i:sThis common placeholder is instead usedA specific reference time layout:2006-01-02 15:04:05.999999999 -0700 MST. Just remember the meaning of each number and symbol in this reference time, and then recombine it according to your needs.

To implement a 12-hour time format with AM/PM indicators, we mainly focus on the reference time in3andPM.

  • 3which represents 12-hour time (without leading zeros).
  • 03Represents a 12-hour time format with a leading zero (for example03).
  • PMIt is a key in Go language used to indicate AM or PM, it will automatically convert the time toAMorPM.

Now, let's see how to combine these elements to achieve the desired effect:

  1. Pure 12-hour time (with AM/PM): If you want to display the time with a morning/afternoon indicator, you can write it like this:{{stampToDate(item.CreatedTime, "3:04 PM")}}This will produce a similar output.3:30 PMor9:00 AMformat.

  2. 12-hour time with leading zero (with AM/PM)If you want the hour to always be two digits (for example03 PM), then use:{{stampToDate(item.CreatedTime, "03:04 PM")}}This will produce a similar output.03:30 PMor09:00 AMformat.

  3. Combine the date with 12-hour time (with AM/PM)In practice, we usually combine the date and time. For example, displaying "October 26, 2023, 03:30 PM":{{stampToDate(item.CreatedTime, "2006年01月02日 下午03:04")}}Or, a more common English format: "Oct 26, 2023 03:30 PM":{{stampToDate(item.CreatedTime, "Jan 02, 2006 03:04 PM")}}You can also adjust the display of the month and date as needed, for example01/02/2006 03:04 PM.

Practice applying formatted time to content

In AnQiCMS, timestamp data is usually present in various content models (such as articles, products) or comments. For example, the publishing time of an article can be accessed througharchive.CreatedTimeoritem.CreatedTimeRetrieve.

Assuming you are designing a detailed page template for an article and want to display the publish date and time with AM/PM below the title, you can write the template code like this:

<article>
    <h1>{{ archive.Title }}</h1>
    <div class="article-meta">
        发布于:<span>{{stampToDate(archive.CreatedTime, "2006年01月02日 03:04 PM")}}</span>
        <!-- 或者更简洁的英文格式 -->
        <!-- 发布于:<span>{{stampToDate(archive.CreatedTime, "Jan 02, 2006 3:04 PM")}}</span> -->
        <!-- 您也可以获取更新时间并格式化 -->
        <!-- 更新于:<span>{{stampToDate(archive.UpdatedTime, "2006年01月02日 03:04 PM")}}</span> -->
    </div>
    <div class="article-content">
        {%- archiveDetail articleContent with name="Content" %}
        {{articleContent|safe}}
    </div>
</article>

This code will retrieve the title of the current article and itsCreatedTimeTimestamp formatted as "2006-01-02 03:04 PM" time.If the current time is in the afternoon, it will display "afternoon", and if it is in the morning, it will display "morning".

In this way, you can not only make the boring timestamp more intuitive and readable, but also flexibly adjust the time display format according to the style of the website and the target user group, thereby improving the overall quality of content presentation.

Summary

stampToDateTags are a powerful tool in AnQiCMS template development, providing strong support for timestamp formatting. Especially by understanding the time formatting rules of the Go language and using it skillfully3:04 PMThis layout allows us to easily implement a 12-hour time display with AM/PM indicators.This not only makes the content of the website more professional, but also greatly optimizes the user's reading experience.Master this skill, and it will make your AnQiCMS website more refined in detail.


Frequently Asked Questions (FAQ)

  1. Why does AnQiCMS use such a special date format string (such as "2006-01-02 15:04:05") instead of the commonY-m-d H:i:s?AnQiCMS is developed based on the Go language, the time formatting of the Go language uses a fixed "reference time" to define the layout. This reference time isMon Jan 2 15:04:05 MST 2006, it is usually represented in code as2006-01-02 15:04:05. You do not need to rememberY-m-dwhat these letters represent, but rather remember each number in the reference time (such as2006Represents the year,01Represents the month,02represents the day,15represents hours in 24-hour time,04represents the minute,05Representing seconds) and text (such asPMRepresenting AM/PM) positions and meanings, and then rearrange these elements in the format you want.

  2. I only want to display time in 24-hour format without AM/PM indicators, how can I set it?If your website or users are accustomed to 24-hour time, you just need to use the 24-hour part of the Go language reference time. For example,15representing 24-hour time with a leading zero,04represents the minute,05represents seconds. You can format it like this:

    • {{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}It will output.2023-10-26 15:30.
    • {{stampToDate(item.CreatedTime, "15:04:05")}}It will output.15:30:00.
  3. **

Related articles

`CreatedTime` as a 10-digit timestamp, do you need to multiply it by 1000 before using `stampToDate`?

The mystery of timestamp conversion in AnQi CMS: As an experienced website operator and deep user of AnQi CMS, I know how common and crucial the handling of time data is in daily content management and template development.Especially when using a high-efficiency system like the Anqi CMS built with Go language, understanding and correct use of template tags are particularly important. Recently

2025-11-07

In the AnQi CMS template, can the `stampToDate` function display relative time such as 'X days ago' or 'X hours ago'?

As an experienced website operations expert, I know that flexible time display in content management systems is crucial for improving user experience and content timeliness.Especially displays such relative time as "X days ago", "X hours ago", which allows readers to understand the age of the content at a glance, thus better deciding whether to delve deeper into reading.Today, let's delve into whether the `stampToDate` tag built into the Anqi CMS template can achieve this relative time display, and how we should go about implementing it.--- ## AnQi CMS template in

2025-11-07

How to concatenate the formatted date of the `stampToDate` tag with other text content to form a complete display message?

As an experienced website operation expert, I fully understand that how to flexibly display information is crucial for improving user experience and meeting operation requirements.AnQiCMS (AnQiCMS) is an efficient and easy-to-use content management system that provides powerful template tag functions, among which the `stampToDate` tag is a tool to process timestamps and format them into readable date strings.

2025-11-07

How to use `stampToDate` to format the creation time of associated documents in `tagDataList`?

As an experienced website operations expert, I am well aware of the importance of flexible data display and user experience.AnQi CMS with its powerful customization capabilities provides many conveniences for content operations.Today, let's delve deeply into a very practical skill in daily content presentation: how to elegantly format the creation time of associated documents in `tagDataList`. ### Timestamp in AnQi CMS template: Why is formatting necessary?In Anqi CMS, whether it is articles, products, or other content models

2025-11-07

How to print the original input timestamp value of `stampToDate` when debugging the AnQi CMS template?

As an experienced website operations expert, I am well aware that AnQiCMS (AnQiCMS) brings great convenience to small and medium-sized enterprises and content operation teams with its efficient Go language architecture and rich features.System design focuses on practicality and scalability, especially in terms of template customization and content presentation, providing us with great freedom.In daily content operation and maintenance, template debugging is an inevitable part, and correctly obtaining and understanding the original data of variables in the template is crucial for efficient troubleshooting. Today

2025-11-07

Does the `stampToDate` tag have different formatting capabilities between different versions of Anqi CMS?

As an experienced website operations expert, I am deeply familiar with the various functions and content operation strategies of AnQi CMS, and I am happy to analyze the time formatting capability of the `stampToDate` tag across different versions of AnQi CMS.This tag plays an important role in daily content display, especially for websites that need to finely control the display format of time, where stability and functional characteristics are of great concern to operators.### AnQi CMS `stampToDate` tag time formatting capability

2025-11-07

How to ensure that `stampToDate` outputs a consistent date format on different server operating systems (such as Linux/Windows)?

In website operation, the display of content publishing time is often one of the details that users pay attention to. Ensuring that these dates and times are presented in a consistent format across different server environments (such as the familiar Linux and Windows) is an important factor in improving user experience and maintaining website professionalism.As an experienced operator of AnQi CMS, I am well aware of its powerful capabilities based on the Go language, especially the unique advantages of the template tag `stampToDate` in dealing with such issues.Today, let's delve deeper

2025-11-07

In the `tag-stampToDate.md` example, how is the `publishStamp` variable defined, and can it be used in practice?

As an experienced website operations expert, I am well aware of the importance of content timeliness and display methods for user experience and SEO optimization in my daily work.AnQiCMS (AnQiCMS) provides great convenience for us with its flexible template engine and rich tag system.TodayReveal

2025-11-07