How to convert the `LastLogin` timestamp obtained from the `userDetail` tag into a readable format using `stampToDate`?

Calendar 👁️ 53

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.

Related articles

Why does AnQi CMS recommend using `stampToDate` to handle timestamps instead of using the `date` filter directly?

In the world of AnQi CMS templates, we often encounter situations where we need to format and display time data.Whether it is the publication time of the article, the update date of the product, or the submission moment of user comments, a clear and readable date format is crucial for the website user experience.AnQi CMS is a powerful content management system developed based on the Go language, which is SEO-friendly and provides an efficient and clear strategy for handling time data.

2025-11-07

What is the core difference between the `stampToDate` and the `date` filter mentioned in `tag-filters.md`?

As an experienced website operations expert, I know that the accurate display of time information is crucial for user experience and SEO optimization.In the powerful template system of AnQiCMS, we often encounter scenarios where we need to format time data for output.At this moment, `stampToDate` and `date` are two tools that seem to have similar functions and appear in our minds.However, there is a core distinction between them, understanding this distinction is crucial for efficient and error-free template development.

2025-11-07

In the `archiveList` loop, how does `stampToDate` ensure that each `item.CreatedTime` is formatted correctly?

## Mastering Time: The Art of Date Formatting in AnQiCMS' `archiveList` Loop In the world of content management, information such as the publication time and update time of website content is not only a key element for providing context to readers, but also an important indicator of a website's professionalism and user experience. For AnQiCMS, an enterprise-level content management system based on Go language and focusing on efficiency and flexibility, how to elegantly present the original timestamps stored in the database to users is a significant issue.

2025-11-07

Does the `stampToDate` tag support formatting timestamps into the international date format "MM/DD/YYYY"?

In the daily operation of AnQiCMS, the formatting of the Unix timestamp is a common requirement, especially when our website content needs to be targeted at international users, the flexibility of the date display format is particularly important.Today, let's delve deep into the `stampToDate` tag in AnQiCMS to see if it can meet our needs to format timestamps into the international date format of "MM/DD/YYYY".

2025-11-07

How to use `stampToDate` to display the `ExpireTime` deadline of VIP members in the user center?

## Security CMS Practical: Display VIP membership expiration date elegantly in the user center, say goodbye to timestamp troubles!In modern content operation, the VIP membership system is an important link in enhancing user stickiness and achieving content monetization.The AnQi CMS, with its flexible user group management and VIP system, allows you to easily build paid content or membership services.However, how to display the `ExpireTime` (expiration time) of VIP members in a way that is intuitive and easy to understand for users, to avoid the confusion caused by the original timestamp, is the key to improving user experience.

2025-11-07

`prevArchive` and `nextArchive` tags get the document time, how to use `stampToDate` to unify the format?

In the daily operation of Anqi CMS, we are well aware that the details of content presentation often determine the quality of user experience.A high-quality article is not only engaging in content, but also crucial in terms of layout and information presentation professionalism.Among them, the unified formatting of the article publication time is a detail that is small but can greatly enhance the professionalism of the website.

2025-11-07

Can the `stampToDate` tag handle invalid or non-10-digit timestamps and what will it return?

As a senior website operations expert, I am well aware of AnQiCMS's efficiency and flexibility in content management.It provides us with rich and practical template tags, allowing us to build web pages with great freedom.Among them, the `stampToDate` tag is undoubtedly an important tool for handling time display, it can transform the boring Unix timestamp into the date and time format we are familiar with.However, any tool has its predefined boundaries of use, and it is particularly important to understand its behavior patterns when the input data does not meet expectations.

2025-11-07

How to avoid page display exceptions caused by `stampToDate` format string error in AnQi CMS template?

As an experienced website operations expert, I am well aware that every detail of website content management is related to user experience and brand image.AnQiCMS (AnQiCMS) benefits from its efficient architecture based on the Go language and its flexible template system, providing strong support for content management.However, even the most excellent system may encounter some small challenges in the customization process.

2025-11-07