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.An original timestamp number is often difficult for ordinary users to understand, far less friendly than the intuitive and easy-to-read 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 convert this technical data into practical information close to user habits.

Today, let's delve into how to utilizeuserDetailto obtain the timestamp of the last login time of the users labeledstampToDateThis magical built-in filter transforms it into a readable format that everyone can easily understand.

Get the original timestamp of the last login:userDetailmade its debut

In the AnQi CMS, to obtain detailed user information,userDetailtags are our powerful assistants. It allows us to accurately extract user data based on parameters such as user ID,LastLoginField records the time of the user's last login, but it is usually stored in the form of Unix timestamp.

For example, if you want to get the last login timestamp of the 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,userLoginInfoThe variable will store a pure number, such as1678886400.Although for the system, this is an accurate and error-free time point, but for front-end display and user reading, this is obviously not an ideal form.We need to translate it into the familiar year, month, day, hour, minute, and second.

The magic of conversion:stampToDateappearance

It is precisely to solve this problem of 'incomprehensible number language' that Anqi CMS providesstampToDateThis powerful timestamp formatting label. Its core function is to convert a Unix timestamp into a highly readable date-time string according to the specified format.

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 format string follows the Go language's unique time formatting standard, rather than the common oneYYYY-MM-DDEnglish.2006-01-02 15:04:05(i.e., at 3:04:05 PM on January 2, 2006).This means, you want to display which part (such as year, month, day) in the output, then write the corresponding number (2006, 01, 02, etc.) in the corresponding position of the format string.

Let's see some common Go language time formatting examples:

  • Year: 2006
  • Month: 01
  • Date: 02
  • Hour (24-hour format): 15
  • Minutes: 04
  • Seconds: 05
  • Day of the week: Mon(Abbreviation for Monday),Monday(Full name of Monday)
  • Month name: Jan(Abbreviation for January),January(Full name for January)

By combining these reference numbers, we can almost build any date-time format we want.

Combine both into one: practical examples

Now, let's takeuserDetailThe one obtainedLastLoginTimestamps withstampToDateCombine them and see how to flexibly display a user's last login time in a 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,currentUserVariables fromuserDetailLabels were obtained fromLastLoginThe timestamp value is then directly passed as the first argument tostampToDate.By adjusting the second formatting parameter, we can easily convert the original timestamp into various easily readable date and time formats, whether it is a simple date, or one with specific hours, minutes, and seconds, or even presented in Chinese, it becomes effortless.

Summary

Anqi CMS'suserDetailandstampToDate


Common Questions (FAQ)

1. If I only need to display the date and not the specific time,stampToDateHow should I use it?

答:非常简单。You only need to omit the time-related part in the Go language formatted string and keep the reference numbers related to the date only."2006-01-02"If you want to display "2006-01-02" in Chinese format, you can use"2006年01月02日".

2.stampToDateDoes the tag support a timestamp of 10 digits or 13 digits?

答:According to the document description of Anqi CMS,stampToDatethe label expects to receive10-digitof Unix timestamp, representing the number of seconds since the Unix epoch (00:00:00 UTC on January 1, 1970), 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 first.

3. Can I get and format the publishing time of multiple documents in a loop?

答:Of course you can. InarchiveListorpageListTags used for loop output, each loop item (for example)itemusually contains)CreatedTimeorUpdatedTimeWait for the timestamp field. You just need to takeitem.CreatedTimeoritem.UpdatedTimeasstampToDatethe first parameter.{{ stampToDate(item.CreatedTime, "2006-01-02") }}This makes it very convenient to format time data in bulk.