Unlock time magic: Easily display 'which day of the week' in Anqi CMS template
As an experienced website operations expert, I know the importance of content timeliness and presentation for user experience and information communication.In such a flexible and efficient content management system as AnQiCMS, we can not only easily manage a variety of content, but also finely control every detail of content display through its powerful template engine.stampToDateThe function converts timestamps into readable dates and further extracts and displays the corresponding 'which day of the week'.
AnQiCMS template: The combination of efficiency and flexibility
AnQiCMS's template system adopts a syntax similar to the Django template engine, which means you can build the layout and content of the page with concise and intuitive tags and variables. Such as{{变量}}Used for output data, and{% 标签 %}It is used to implement logical control or call specific functions.This design philosophy allows non-technical operation personnel to master the essentials of template modification through simple learning, thus quickly responding to the changes in content display requirements.
Our template files are usually stored in/templatethe directory and use.htmlAs a suffix. In these templates, you will frequently deal with various built-in tags and functions provided by AnQiCMS,stampToDatewhich is one of the tools for processing time data.
stampToDateThe Art of Timestamp Conversion Unveiled
In website content, especially in news articles, event schedules, or update logs, we often encounter time data stored in the form of timestamps (usually 10-digit Unix timestamps, representing the number of seconds from 1970-01-01 00:00:00 UTC to the present).These numbers displayed directly are meaningless to the user.stampToDateThe emergence of functions is to solve this pain point
Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}The key is the second parameter - 'format'. Unlike what we commonly useyyyy-MM-ddThe format is slightly different, AnQiCMS is developed based on Go language, so its time formatting follows the unique Go language formatReference time formatThis reference time is a fixed value:2006年1月2日 15点04分05秒You need to match the output format you expect with the corresponding part of this reference time.
For example, if you want to display2023-10-26 10:30:00The format string should be"2006-01-02 15:04:05".
Core technique: How to get and display the day of the week
Now, let's solve the core problem of the article: how tostampToDateGet and display the day of the week corresponding to the timestamp.
In the date formatting rules of the Go language, to represent the day of the week, we can use the following two reference values:
MondayThis will output the full name of the week, for example, "Monday", "Tuesday".MonThis will output abbreviated names of weekdays, such as 'Mon', 'Tue.'
Therefore, to get a timestamp (such as the publication time of an articleitem.CreatedTimeWe can extract the day of the week and display it on the template like this:
Suppose you are iterating through a list of articles, and each article's data object isitemwhich contains aCreatedTimeThe field stores a 10-digit timestamp.
If you want to display the full name of the week, such as 'Monday', 'Tuesday', you can write it like this:
{# 假设item.CreatedTime是一个10位时间戳 #}
<p>发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日 Monday") }}</p>
After running, the page may display content similar to "Publish date: October 26, 2023, Thursday".
If you prefer to display abbreviated names of weekdays such as "Monday", "Tuesday", you can useMonas part of the format:
<p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02 Mon") }}</p>
The page will display the effect of "Published date: 2023-10-26 Thu".
You can also just display the day of the week without the date part, just keep the format string.MondayorMonand it is done:
<p>今天是:{{ stampToDate(item.CreatedTime, "Monday") }}</p>
This will output: "Today is: Thursday."
This technique can be applied to any AnQiCMS data field that contains a timestamp, such as the document'sCreatedTime(Creation time) orUpdatedTime(Update time), even any timestamp field defined in the custom content model.It greatly enhances the readability and user experience, making your website information more friendly and easy to understand.
Practical application scenarios and precautions
Integrating weekdays into your content display can bring many benefits:
- Enhance user experienceUsers do not need to calculate it themselves, they can obtain date information at a glance, which is crucial for content such as events, news, or blog articles.
- Enhance the value of informationFor example, clearly marking the day of the week when publishing weekly reports or regular updates helps users quickly locate information.
- Optimize content presentationMake date information more complete and humanized, in line with daily reading habits.
While usingstampToDateWhen defining a function, there are several points to pay special attention to:
- Formatting rules for the Go language: Always remember the unique reference time format of Go language
2006-01-02 15:04:05. All format strings should be based on this, not the commonY-m-detc. - Timestamp digit:
stampToDateThe function expects a 10-digit Unix timestamp (seconds).If your time data is a 13-digit millisecond timestamp, you may need to divide it by 1000 first, convert it to 10 digits, and then format it. - Variable type confirmation: Make sure you pass to
stampToDateThe first parameter is indeed a valid numeric type timestamp. AnQiCMS built-inCreatedTimeandUpdatedTimefield is usually stored as a 10-digit timestamp and can be used directly.
MasteredstampToDateThis function's trick will allow you to control the display of time information on the AnQiCMS website more flexibly and accurately, enhancing your content operation strategy to a higher level.
Frequently Asked Questions (FAQ)
Why do I use
stampToDateWhen displaying the day of the week, the output is English instead of Chinese?AnQiCMS'stampToDateThe function is based on the built-in time formatting feature of the Go language, which defaults to outputting the English names of weekdays (such as "Monday" or "Mon"). To display the Chinese weekday names (such as "Monday"),stampToDateThe function does not provide this feature directly. You may need to combine it with front-end JavaScript for localization conversion, or through the AnQiCMS template,{% if %}Or customize the mapping logic to output the corresponding Chinese based on the English day of the week.I have a 13-digit timestamp, how do I use it directly?
stampToDateHow do I fix the incorrect display after?stampToDateThe function is designed to handle a 10-digit Unix timestamp (seconds).If you have a 13-digit millisecond timestamp, you need to divide it by 1000 before passing it to the function.item.MillisecondTimeYou may need to handle it like this:{{ stampToDate(item.MillisecondTime / 1000, "Monday") }}.In addition to the days of the week, I can also use
stampToDateWhat other date and time information can the function obtain?Of course! Just refer to the reference time in the Go language2006-01-02 15:04:05You can combine almost any date and time format you need. For example:"2006": Year (four digits)"01": Month (with leading zero)"_1"Month (without leading zero, with space for single digits)"02"Date (with leading zero)"_2"Date (without leading zero, with space for single digits)"15"Hour (24-hour format, with leading zero)"03": Hour (12-hour format, with leading zero)"04": Minute (with leading zero)"05": Second (with leading zero)"PM"or"pm": AM/PM indicator"MST"or"Z07:00": Time zone information By flexibly applying these rules, you can achieve various complex date and time display requirements.