In the daily operation of AnQi CMS, we often need to display various data information to users, and the presentation of time is particularly important.A raw timestamp number is often difficult for ordinary users to understand, far less friendly than the intuitive date and time format.Imagine if the user sees their 'last login time' as a string of disordered numbers, the experience would naturally be greatly reduced.AnQi CMS is well-versed in this, providing powerful template tags to help us transform these technical data into practical information close to user habits.
Today, let's delve into how to utilizeuserDetailtags to obtain the timestamp of the last login time of the user and throughstampToDateThis magical built-in filter converts it into a readable format that everyone can easily understand.
Get the original last login timestamp:userDetailmade its debut.
In AnQi CMS, to obtain user details,userDetailtags are our powerful assistants. It allows us to accurately extract user data based on user ID and other parameters. Among them,LastLoginThe field records the user's last login time, but it is usually stored in the form of a Unix timestamp.
For example, if you want to get the last login timestamp of a user with ID 123, the template code might look like this:
{% userDetail userLoginInfo with name="LastLogin" id="123" %}
<p>原始最后登录时间戳:{{ userLoginInfo }}</p>
After this code is executed,userLoginInfoA variable will store a pure number, such as1678886400. Although for the system, this is an accurate time point, it is obviously not an ideal form for front-end display and user reading.We need to translate it into the familiar year, month, day, hour, minute, and second.
The magic of conversion:stampToDateThe debut of
It is to solve this kind of problem that the 'number language is not understood', Anqi CMS providesstampToDateThis powerful timestamp formatting label. Its core function is to convert a Unix timestamp, according to the specified format, into a highly readable date and time string.
stampToDateThe usage is very intuitive, it accepts two parameters: the first is the timestamp to be converted, and the second is the string defining the output format. The basic syntax is:{{stampToDate(时间戳, "格式")}}.
It is especially important to note here,stampToDateThe formatting string follows the unique time formatting standard of the Go language, rather than the common oneYYYY-MM-DDwait. In Go language, string formatting is defined by a specific "reference time", which is2006-01-02 15:04:05(i.e., January 2, 2006, 3:04:05 PM).This means, if you want to display a part of the output (such as year, month, day), you will write the corresponding number in the reference time (2006, 01, 02, etc.) in the format string at the corresponding position.
Let's take a look at some common Go language time formatting examples:
- Year:
2006 - Month:
01 - Date:
02 - Hour (24-hour format):
15 - Minute:
04 - seconds:
05 - When is the day:
Mon(Abbreviation of Monday),Monday(Full name of Monday) - Month names:
Jan(Abbreviation of January),January(January full name)
By combining these reference numbers, we can almost build any date and time format we want.
Combine them into one: a practical example
Now, let's takeuserDetailObtainedLastLoginTimestamp withstampToDateCombine to see how to flexibly display the user's last login time in the template.
{% userDetail currentUser with name="LastLogin" id="123" %}
<p>用户ID:123</p>
<p>
最后登录日期(年-月-日):
<span>{{ stampToDate(currentUser, "2006-01-02") }}</span>
</p>
<p>
最后登录时间(年-月-日 时:分:秒):
<span>{{ stampToDate(currentUser, "2006-01-02 15:04:05") }}</span>
</p>
<p>
最后登录时间(中文格式):
<span>{{ stampToDate(currentUser, "2006年01月02日 15时04分") }}</span>
</p>
<p>
最后登录时间(仅显示时:分):
<span>{{ stampToDate(currentUser, "15:04") }}</span>
</p>
<p>
最后登录时间(包含星期):
<span>{{ stampToDate(currentUser, "2006-01-02 Mon 15:04") }}</span>
</p>
In this code block,currentUserVariables fromuserDetailRetrieved from the tagLastLoginTimestamp value, then passed directly as the first parameter tostampToDate. By adjusting the second formatting parameter, we can easily convert the original timestamp into various readable date and time formats, whether it is a concise date, or one that includes specific hours, minutes, and seconds, or even a Chinese presentation, it becomes effortless.
Summary
Of Security CMSuserDetailandstampToDateThe combination of tags provides an efficient and flexible solution for website operators to handle timestamp data.It not only helps us easily obtain the key time information of users, but also presents it in a user-friendly way on the front-end page. Whether it is the user's personal center, admin logs, or other scenarios that require displaying time information, it can greatly improve the website's user experience and data readability.You can make the data display of the website more professional and intelligent by mastering the usage of these two tags.
Frequently Asked Questions (FAQ)
1. If I only need to display the date and not the specific time,stampToDatehow should I use it?
Answer: It is very simple. You just need to omit the part related to time in the Go language formatting string and keep only the reference numbers related to the date.For example, if you want to display only "year-month-day", the format string can be written as"2006-01-02";If you want to display the Chinese format of “2006年01月02日”, you can use"2006年01月02日".
2.stampToDateThe label supports a timestamp of 10 or 13 digits?
Answer: According to the Anqi CMS documentation,stampToDateThe label is expected to receive10 digitsA Unix timestamp represents the number of seconds since the Unix epoch (January 1, 1970, 00:00:00 UTC), for example1609470335. If you encounter a 13-digit timestamp (usually including milliseconds), you may need to convert it to a 10-digit second-level timestamp in another way.
3. Can I get and format the publish time of multiple documents in a loop?
Of course you can. InarchiveListorpageListTags used for looping output content, each loop item (such asitem) usually containsCreatedTimeorUpdatedTimeWait for the timestamp field. You just need to specifyitem.CreatedTimeoritem.UpdatedTimeasstampToDatethe first parameter. For example:{{ stampToDate(item.CreatedTime, "2006-01-02") }}This makes it very convenient to format time data in bulk.