How to ensure that the time zone is displayed as local time instead of UTC time when the `stampToDate` label formats time?

Calendar 👁️ 73

AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, whose powerful template engine makes content display flexible and diverse. In daily content operations, the formatted display of time is a common requirement, andstampToDateThe label is just for this. It can convert Unix timestamps into the familiar date and time format. However, many operators may encounter a confusion: why usestampToDateFormatted time, sometimes it is displayed as UTC time instead of the local time of my website?Today, let's delve deeply into this issue and provide practical solutions.

Get to knowstampToDateLabel and its working principle

In the template design of AnQi CMS,stampToDateA label is a very practical tool that allows us to easily convert 10-digit Unix timestamps stored in databases into various readable date and time formats. Its basic usage is simple and straightforward:{{stampToDate(时间戳, "格式")}}. The 'timestamp' usually comes from the creation time of the document (item.CreatedTime) or update time(item.UpdatedTime) etc. fields, and the 'format' follows the time formatting rules specific to the Go language (for example"2006-01-02 15:04:05")

We can see from the document description that,stampToDateThe core function of the label is to "format timestamps". It receives a digital timestamp and then outputs a string in the specified format.The key is that this tag does not have a direct parameter to explicitly specify 'Please display time in a specific time zone'.This means that its output behavior largely depends on how it internally interprets this timestamp, as well as how the time zone is configured on the server environment where AnQi CMS is running.

Why does it show UTC time?

Understand this question, we need to review the essence of the timestamp.Unix timestamp (also known as POSIX time) is usually the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).This means, the timestamp itself isIt does not contain timezone informationIt is just a number representing a moment in time.

AnQi CMS, like many modern content management systems, usually uses UTC timestamps when storing time in the database.This is a common and recommended practice because it avoids data confusion in multi-time zone scenarios, ensuring the consistency and accuracy of time data.

WhenstampToDateWhen a label receives a UTC timestamp and formats it, if the underlying Go application does not explicitly perform timezone conversion, or if the server environment is set to UTC by default, it will directly format the UTC timestamp as a string representing UTC time.For users in the East Eighth Zone (such as Beijing time), this will cause the displayed time to be 8 hours earlier than the local time, thus creating a "time is wrong" illusion.

EnsurestampToDateSolution to display local time

SincestampToDateThe tag itself does not have a direct timezone parameter, so in order to make it display local time, our focus needs to be onThe server environment running AnQi CMSOn. The Go language application reads the system's timezone settings by default and processes time localization accordingly.Therefore, the most direct and effective method is to ensure that the time zone configuration of your secure CMS server is correct.

1. Configure the server time zone

This is usually the first step in solving time display issues. Whether it is a Linux or Windows server, you need to set its time zone to the target local time zone of your website.

  • For Linux servers (such as CentOS, Ubuntu):You can usetimedatectlCommand to manage system time zones. First, check the current time zone:

    timedatectl status
    

    Then, list all available time zones:

    timedatectl list-timezones
    

    Find the time zone you need (for exampleAsia/ShanghaiorAsia/Hong_Kong), then set it:

    sudo timedatectl set-timezone Asia/Shanghai
    

    Run again after setting uptimedatectl statusConfirm that the time zone has changed.

    For some older systems or nonetimedatectlcases, you may need to create or link manually/etc/localtimethe file and set upTZEnvironment variable.

  • For Windows server:In Windows system, you can change the time zone by the "Date and Time" settings.Right-click on the time in the taskbar, select "Adjust date/time", and then select the correct time zone in the settings window.

2. Restart the Anqicms service

After changing the server time zone, simply saving the settings is not enough.AnQi CMS as a Go application, it will load the system environment's time zone configuration when starting.Therefore, you mustRestart the Anqicms service, let it reread and apply the new timezone settings.

If you are using panel tools like Baota or 1Panel, you can find the restart button in the corresponding Go project management or container management interface. If you start manually through a script, you need to stop the service first (such as runningstop.sh), then start the service (run)start.sh)

3. Verify time display

Refresh your website page and check again after restarting the servicestampToDateFormatted time. They should now be displayed correctly as your local time.

To verify more accurately, you can also try using formats that include timezone offsets in the template, such as{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05 -0700 MST")}}.-0700 MSTThe timezone offset and timezone name will be displayed, which can help you confirm whether the Go application has correctly identified the local timezone. For example, if it shows as+0800 CSTIt indicates that it has been correctly identified as China Standard Time (CST, UTC+8).

Advanced considerations: Front-end JavaScript auxiliary display.

Although the server-side configuration is to resolvestampToDateThe fundamental method of time zone problems, but in some specific scenarios, you may also want to handle the display of time on the front end through JavaScript, for example, if your website users are spread all over the world and you want the time to be displayed according toThe time zone of the user's browserTo dynamically display.

In this case, you can continue to letstampToDateOutput a UTC timestamp (or a Go standard UTC formatted string), then pass this value to the front-end JavaScript. In JavaScript, you can useDatethe object'stoLocaleString()ortoLocaleDateString()Methods, they will automatically display the time according to the user's browser settings in the local time zone.

For example, in the template:

<span data-timestamp="{{ archive.CreatedTime }}"></span>

In JavaScript:

Related articles

How to format output with `stampToDate` and then define a variable with `with` tag for reuse?

Dear Anqi CMS operation partners, hello! As an expert who deeply understands the integration of content operation and technology, I know the value of an efficient and flexible content management system for our daily work.AnQi CMS, with its high-performance architecture in Go language and Django-style template engine, undoubtedly provides us with powerful content display capabilities.Today, let's delve into a common and practical little trick in template design: how to cleverly define variables using the `with` tag after formatting output time with `stampToDate`

2025-11-07

Can the `stampToDate` label handle the millisecond precision of Unix timestamps?

In a content management system, the accurate display of time is crucial, especially when dealing with user behavior, data logs, or the release of specific events.AnQiCMS as a powerful content management system based on the Go programming language provides rich template tags to meet various content display needs, among which the `stampToDate` tag is a powerful tool for formatting timestamps.However, whether this tag can handle the millisecond level precision of Unix timestamps is a concern for many operators and developers.We can see from the official documentation of AnQiCMS

2025-11-07

How can the scheduled publishing function of AnQi CMS accurately display the publish time on the front end through `stampToDate`?

As an experienced website operation expert, I am well aware of the art of content release timing and its profound impact on user experience, search engine optimization, and even operation efficiency.AnQiCMS (AnQi CMS) is exactly born to meet these needs, and its 'Time Factor-Scheduled Publication Function' is a powerful tool in the hands of content operators.But having a strong publishing capability is not enough, how to present these carefully set publishing times elegantly and accurately to the readers is equally important.This is the place where the `stampToDate` template tag really shines.### Schedule Post Function

2025-11-07

Can `stampToDate` format timestamps into ISO 8601 standard format (such as "YYYY-MM-DDTHH:MM:SSZ")?

As an experienced website operation expert who deeply understands the operation of AnQiCMS, I fully understand the importance of time formatting, especially standardized time formats, in content management.Today, let's delve into the `stampToDate` tag of Anqi CMS to see if it can format timestamps into ISO 8601 standard format, such as the `"YYYY-MM-DDTHH:MM:SSZ"` we often see.### AnQi CMS's `stampToDate`: Easily format timestamps to ISO

2025-11-07

Is there an easy way to check if the input timestamp of `stampToDate` is valid in the AnQi CMS template?

As an experienced website operation expert, I am well aware that in a content management system like AnQiCMS, the accuracy and display format of timestamps are crucial for user experience and data presentation.`stampToDate` this powerful label function is undoubtedly a good helper for us to process time information in the template.However, like any tool, it also requires us to use and understand its input requirements correctly.Today, let's delve deeply into a problem that everyone may encounter in the development of Anqi CMS templates: **'In the Anqi CMS template

2025-11-07

How to use `stampToDate` to format the creation time of associated documents in `tagDataList`?

As an experienced website operations expert, I am well aware of the importance of flexible data display and user experience.AnQi CMS with its powerful customization capabilities provides many conveniences for content operations.Today, let's delve deeply into a very practical skill in daily content presentation: how to elegantly format the creation time of associated documents in `tagDataList`. ### Timestamp in AnQi CMS template: Why is formatting necessary?In Anqi CMS, whether it is articles, products, or other content models

2025-11-07

How to concatenate the formatted date of the `stampToDate` tag with other text content to form a complete display message?

As an experienced website operation expert, I fully understand that how to flexibly display information is crucial for improving user experience and meeting operation requirements.AnQiCMS (AnQiCMS) is an efficient and easy-to-use content management system that provides powerful template tag functions, among which the `stampToDate` tag is a tool to process timestamps and format them into readable date strings.

2025-11-07

In the AnQi CMS template, can the `stampToDate` function display relative time such as 'X days ago' or 'X hours ago'?

As an experienced website operations expert, I know that flexible time display in content management systems is crucial for improving user experience and content timeliness.Especially displays such relative time as "X days ago", "X hours ago", which allows readers to understand the age of the content at a glance, thus better deciding whether to delve deeper into reading.Today, let's delve into whether the `stampToDate` tag built into the Anqi CMS template can achieve this relative time display, and how we should go about implementing it.--- ## AnQi CMS template in

2025-11-07