How to customize the formatting of the comment publishing time obtained from the `commentList` tag?

Calendar 👁️ 71

In website operation, the comment module is not only an important battlefield for user communication, but also an embodiment of the vitality of the content ecosystem.A clear and readable comment posting time, which can greatly enhance user experience, allowing visitors to grasp the freshness of the information at a glance.However, many content management systems (CMS) often output the comment time in an unformatted timestamp by default in their templates, which is hard and not intuitive for ordinary users to read.

AnQiCMS (AnQiCMS) is an efficient and highly customizable content management system that takes this into full consideration. EvencommentListTags when retrieving comment data, itsCreatedTimeThe field returns the original timestamp, AnQiCMS also provides a very flexible and easy-to-use way to format it into various date and time formats that we are familiar with and like. This is due to its powerful template engine and built-instampToDate.

Get to knowcommentListofCreatedTime

When we go throughcommentListThe tag displays comments in the template, each comment data includes aCreatedTimefield, for example:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      {# 如果直接输出,可能会看到这样的原始时间戳:1678886400 #}
      <span>发布时间:{{item.CreatedTime}}</span>
      {# ... 其他评论内容 ... #}
    </div>
    {% endfor %}
{% endcommentList %}

Such an original timestamp is accurate, but it is unreadable to users. To convert it to a format like 'March 15, 2023 10:30' or '10:30 yesterday', it is necessary to use the 'time magic' in the AnQiCMS template engine...stampToDate.

stampToDateThe charm of tags

stampToDateIs AnQiCMS a powerful tool specifically used for timestamp formatting.Its usage is very intuitive, just pass in two parameters: the timestamp to be formatted and the date-time format string you want to present.

Its basic syntax is:{{stampToDate(时间戳, "格式")}}.

It should be emphasized that AnQi CMS is developed based on Go language, and therefore its time formatting follows the unique specification of Go language. Unlike many other languages that use placeholders such as "Y-m-d H:i:s", Go language adopts a fixed reference time "2006-01-02 15:04:05.999999999 -0700 MSTas a formatting template. You can use the components of this reference time to build any format you want. Simply put:

  • 2006Represents the year
  • 01Represents the month
  • 02Represents the date
  • 15represents 24-hour time format (can also be03represents 12-hour time format)
  • 04representing minutes
  • 05representing seconds
  • .999999999represents nanoseconds
  • -0700represents timezone offset
  • MSTrepresents timezone name

Mastered this rule, you can define the time display format at will.

Practice: Customize the comment time format

Next, let's look at several common examples to see how tocommentListinCreatedTimeto customize the format.

Example one: only display the year, month and day

If we only need to display the publication date of the comment, in the format of

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span> {# 输出:2023-03-15 #}
      {# ... 其他评论内容 ... #}
    </div>
    {% endfor %}
{% endcommentList %}

Example two: Display the full date and time

If you need to display the complete date and time to the second, the format is "Year-Month-Day Hour:Minute:Second", you can define it like this:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</span> {# 输出:2023-03-15 10:30:00 #}
      {# ... 其他评论内容 ... #}
    </div>
    {% endfor %}
{% endcommentList %}

Example three: Display Chinese date format

To fit the Chinese reading habits, we can format the date as "Year Month Day":

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> {# 输出:2023年03月15日 10:30 #}
      {# ... 其他评论内容 ... #}
    </div>
    {% endfor %}
{% endcommentList %}

Example four: Only show time (hour:minute)

Sometimes, we might only need to pay attention to the time and minutes a comment was posted:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      <span>发布时间:{{stampToDate(item.CreatedTime, "15:04")}}</span> {# 输出:10:30 #}
      {# ... 其他评论内容 ... #}
    </div>
    {% endfor %}
{% endcommentList %}

By these examples, you can seestampToDateThe strength of the tag, it can meet the vast majority of date and time display needs, greatly enhancing the usability and aesthetics of the comment module.In practice, you can choose the most suitable date and time format according to the overall design style of the website and user preferences.

Summary

The custom comment publishing time format is a small yet beautiful detail that enhances the user experience of the website. Anqi CMS relies on its flexible template engine and simple and intuitivestampToDateTags make this requirement easy. As a website operator, we should make good use of these features, transform boring technical data into user-friendly, pleasing content, and create a more smooth and pleasant browsing environment for visitors.


Frequently Asked Questions (FAQ)

Q1: Why does the time formatting string of AnQiCMS look so special, using "2006-01-02 15:04:05" instead of "YYYY-MM-DD"?

A1:This is because AnQiCMS is developed in Go language. Go language abandoned the traditional "Y-m-d" placeholder at the beginning of its design and adopted a fixed reference time point - "January 2, 2006 at 3:04:05 PM MST (-0700)" which is also2006-01-02 15:04:05 -0700 MST. When you need to format time, you just need to replace the corresponding year, month, day, hour, minute, second numbers in this reference time with the format characters you want (such as replacing the01Replace), Go language will parse and output the corresponding format time according to your template string.Although it looks a bit peculiar at first glance, once you understand its principle, you will find it very intuitive and powerful.

Q2: BesidescommentListofCreatedTime, I amarchiveListorarchiveDetailinCreatedTimeorUpdatedTimealso can be usedstampToDateCan I format it?

A2:Of course you can.stampToDateThe tag is a general time formatting tool provided by the AnQiCMS template engine, it is not limited to specific tags. Whether it isarchiveListThe article was published at (item.CreatedTime),archiveDetailThe article was updated at (archive.UpdatedTime) or any other with Unix time

Related articles

Can AnQiCMS's comment function be set to have different publishing permissions according to user groups, such as only VIP users being able to comment?

As an experienced website operations expert, I fully understand your needs for content interactivity and fine-grained user permission management.In content operation, providing differentiated services based on user identity, such as only allowing VIP users to comment, is an important strategy to enhance user stickiness and achieve content monetization.Today, let's delve into whether AnQiCMS (AnQi CMS) supports setting different publishing permissions based on user groups in the comment feature.

2025-11-06

How to integrate CAPTCHA functionality in the AnQiCMS comment submission form to prevent spam comments?

In today's world of content operation, the website comment area is not only an important place for user interaction, but also often becomes a hotbed of spam and malicious attacks.These annoying spam comments not only affect user experience and reduce the quality of website content, but may also have a negative impact on search engine optimization (SEO).As an experienced website operations expert, I am well aware of the importance of defending against these automated scripts and malicious behaviors.

2025-11-06

Does AnQiCMS support anonymous user comment submission? How does the template handle the display of anonymous commenters?

As an experienced website operations expert, I fully understand the importance of the comment function for website activity and user interaction.Among many content management systems, AnQiCMS has won the favor of many operators with its high efficiency and flexibility.TodayHow do we elegantly display the information of these commenters in the template?### AnQiCMS comment feature analysis

2025-11-06

How to ensure that the administrator receives an immediate reminder notification after submitting a new comment on AnQiCMS?

## Quickly Insight User Voice: AnQiCMS New Comment Reminder Notification Configuration Guide In the daily operation of the website, user interaction is undoubtedly an important indicator of content activity and community health.Every new comment or message, whether it's a unique insight into an article or a consultation feedback on a product or service, contains valuable user voices.As website operators, we must ensure that we can receive these dynamics in a timely manner, so that we can respond quickly and manage efficiently, which not only improves the user experience, but is also the key to maintaining the ecological environment of the website's content.

2025-11-06

How to display the contact name of the website in the AnQiCMS template?

## In AnQiCMS templates, elegantly display the website contact name: A Guide for Experts In today's increasingly popular digital marketing, a website is not just a platform for displaying information, but also a bridge for enterprises to build trust and communicate with users.A clear and easily accessible contact method, especially the name of the website operator, can effectively enhance the professionalism and亲和力 of the website, making visitors feel a sense of reality and responsible attitude.For enterprises using a CMS like AnQiCMS, how to efficiently and flexibly display key information in templates

2025-11-06

How to call the contact phone number set in the AnQiCMS backend to the front page?

As an experienced website operations expert, I am well aware that the accuracy of website information and the convenience of front-end display are crucial to user experience and operational efficiency.AnQiCMS (AnQiCMS) provides a very friendly solution in this regard, especially for the management and retrieval of core information such as company contact details.Today, let's delve into how to accurately and efficiently display the carefully set contact phone number in the AnQiCMS backend on the front page of the website.

2025-11-06

How to obtain the detailed contact address of the website through the AnQiCMS contact information label?

In the digital age, the contact information on a website is not just a string of information, but also a bridge for communication with customers and the foundation for building brand trust.For many website operators who use AnQiCMS, how to efficiently and flexibly display these key information on the website front-end, especially detailed contact addresses, is a common and important issue.Fortunately, AnQiCMS, with its intuitive and powerful template tag system, makes everything extremely simple.### Contact Information Label of AnQi CMS

2025-11-06

How can website operators display the contact email configured in AnQiCMS on the front page?

As an experienced website operations expert, I am well aware of the importance of a website's contact information, especially the contact email, in building user trust, promoting communication, and improving conversion rates.In AnQiCMS (AnQi content management system), displaying these key information efficiently and accurately on the front page is a highlight in the daily work of website operators.Today, let's delve into how to cleverly utilize the powerful functions of AnQiCMS to make your contact email shine on the front page.

2025-11-06