How to format the timestamp label `stampToDate` to display time data in a readable format?

Calendar 👁️ 72

In a content management system, time data is often represented as a string of numbers, which we call a Unix Timestamp. This form is highly efficient for machine processing, but it is not very user-friendly, a string of1678886400This number cannot be intuitively understood to represent a date and time. Anqi CMS knows this and therefore provides a very practical template tagstampToDateHelp us convert these cryptic timestamps into the date and time format we are accustomed to reading in our daily lives.

stampToDateThe clever use of tags

stampToDateThe core function of the tag is to convert a 10-digit or 13-digit timestamp (usually second-level or millisecond-level) into a date and time string that is easy to understand according to the format rules we set. The basic usage of this tag is very intuitive:

{{stampToDate(时间戳, "格式")}}

Here, 时间戳The number you want to convert may come from the publication time of the article (for examplearchive.CreatedTime), the creation time of the comment, or any other data stored in timestamp form. And"格式"Part is to define the appearance of time you want it to ultimately present.

Unique aspects of Go language time formatting:

With other content management systems (such as PHP or Java) through a specific letter (such asYrepresents the year,mrepresents the month,HRepresenting hours) to define different time formats, Anqi CMS is developed based on Go language, therefore its time formatting follows the unique rules of Go language.

Go language uses a fixed reference date and time as a template when formatting time:2006年01月02日 15时04分05秒More precisely, the complete reference time is:

Mon Jan 2 15:04:05 MST 2006

This means that when you want to display the year, month, day, hour, minute, and second information in the output, you need to write the corresponding numbers or abbreviations from the reference date in the format string, so that the system knows which part you want to output and in what form.

For example:

  • year: Use2006
  • month: Use01(Number Month),Jan(Abbreviated Month),January(Full Month Name)
  • Day: Use02(Number Date)
  • hours: Use15(24-hour format),03or3(12-hour format)
  • minute: Use04
  • seconds: Use05
  • AM/PM: UsePM
  • Time Zone: UseMST

The key is to ensure that the numbers and letters in your format string correspond exactly to the reference date format in the Go language. For example, if you want to display a four-digit year, you must use2006instead ofyyyy.

The actual application: make time come alive

After understanding the formatting rules of Go language, we can flexibly apply timestamps to all corners of the website.

Assuming we are building an article list and want to display the publish date of each article. In the template, we can accessarchive.CreatedTimethe creation timestamp of the article.

If you want to display a common date format, for example2023-01-15We can use it like thisstampToDate:

{{stampToDate(archive.CreatedTime, "2006-01-02")}}

If we need more detailed time, including hours, minutes, and seconds, for example2023年01月15日 14:30:45:

{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04:05")}}

Pay attention, here I have directly written the Chinese 'year', 'month', 'day', etc. in the format string, and the system will output it as is.

Sometimes, we may only need the month and date, separated by slashes, for example01/15:

{{stampToDate(archive.CreatedTime, "01/02")}}

Even just the time, for instance下午02:30:

{{stampToDate(archive.CreatedTime, "03:04PM")}}

By these flexible combinations, you can precisely control the display of time data according to the needs of the page design. For example, on a document detail page, you may want to display:

发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}

And on the list page, for brevity, it may only display:

{{stampToDate(item.CreatedTime, "01-02")}}

This makes our website content more vivid and the information transmission clearer.

Summary

By flexible applicationstampToDateThe label and the Go language-specific formatting rules allow Anqi CMS users to easily convert backend timestamp data into user-friendly and diverse date-time displays.Mastering this tag not only enhances the reading experience of website content but also showcases the powerful ability of Anqi CMS in detail handling.No matter what content you have, whether it is a blog article, product release or event news,stampToDateAll of which can help you present time information in ** manner.


Frequently Asked Questions (FAQ)

Q1: Why do I useY-m-d H:i:ssuch format strings not work?A1: Anqi CMS is developed based on Go language, its time formatting follows the unique rules of Go language, rather than the common rules in PHP or Python languagesY-m-dPlaceholder. You need to use the reference date of the Go language.2006-01-02 15:04:05to construct your format string with the corresponding numbers. For example,2006represents the year,01represents the month,02represents the date,15represents the hour in 24-hour format, and so on.

Q2: What should I do if my timestamp is a 13-digit number (millisecond level)?A2:stampToDateThe tag expects to receive a 10-digit (second-level) timestamp. If your timestamp is 13 digits long in milliseconds, you need to divide it by 1000 first to convert it to a second-level timestamp.You can perform simple mathematical operations in the template, such as{{stampToDate(时间戳 / 1000, "格式")}}To handle.

Q3: Can I add the Chinese name of the day of the week or month name in the formatted time?A3: The formatting rules of Go language are based on the reference date, so you can directly insert Chinese into the format string. For example, if you want to display "Monday, January 2, 2023", you need to find the reference value of Go language to represent the day of the week (for example,Mon),then construct{{stampToDate(时间戳, "Mon, 2006年01月02日")}}Make sure your template file is encoded in UTF-8, so that Chinese characters can be displayed correctly.

Related articles

How `if` logical judgment tags and `for` loop iteration tags control the conditional display and repeated display of content?

In the daily operation of Anqi CMS, we often need to decide which content should be displayed according to different business logic, as well as how to efficiently repeat the display of a large amount of similar content.At this time, the `if` logical judgment tag and the `for` loop traversal tag have become indispensable tools in template design.They allow us to flexibly control the conditional display and repetition of content, greatly enhancing the dynamicity and maintainability of the website.### The conditional display tool - `if` logical judgment tag The `if` tag is the basis for conditional content display in Anqi CMS templates

2025-11-07

How does the pagination tag `pagination` provide navigation and display control for long content lists?

In website operation, when our content list (whether it is articles, products, or other information) accumulates to a certain amount, displaying all content on a single page not only significantly affects the page loading speed but may also make visitors feel overloaded with information, making it difficult for them to find the content they are really interested in.At this point, a reasonable pagination mechanism is particularly important. AnQiCMS provides a powerful and flexible `pagination` tag, which helps us provide clear navigation and display control for long content lists, thereby greatly optimizing user experience and website performance.Core Function

2025-11-07

How to display the link list of cooperative website links with the tag `linkList`?

In website operation, friendship links (also known as cooperative website links) play an important role.They not only bring additional traffic to the website, but also help to improve search engine optimization (SEO) performance, enhance the authority and user trust of the website.In Anqi CMS, managing and displaying these links becomes very convenient, mainly due to its powerful template tag system, especially the `linkList` tag.### Flexibly display cooperative websites: `linkList` tag usage and basic principles `linkList`

2025-11-07

How to dynamically generate and display the user's guestbook form with the label `guestbook`?

In website operation, visitor comments and interaction are important for building trust, obtaining feedback, and promoting business development.AnQiCMS (AnQiCMS) understands this need and provides a flexible and powerful message form feature, allowing you to easily generate and display user message forms on your website.This is mainly achieved through its built-in `guestbook` template tag, which not only allows you to customize form fields but also ensures the safety and smooth submission of the form.### Understand `guestbook`

2025-11-07

How `include`, `extends`, and `macro` auxiliary tags optimize the template structure and affect the final display of content?

In website operation, template design is a key link in building user experience and content presentation efficiency.AnQiCMS (AnQiCMS) provides strong support for content management with its flexible template engine.Especially among the auxiliary tags such as `include`, `extends`, and `macro`, they do not directly modify the content itself, but through optimizing the template structure, they profoundly affect the final display effect and maintenance efficiency of the website content.

2025-11-07

How to correctly display Markdown formatted mathematical formulas and flowcharts in AnQiCMS content?

For many content creators, especially when it comes to technical articles, tutorials, or data reports, it has always been a challenge to clearly and accurately display mathematical formulas and complex flowcharts on web pages.The traditional HTML editing method is not only繁琐but also prone to errors.It is fortunate that the new version of AnQiCMS has introduced support for Markdown editors, which brings great convenience to handling such complex content.It makes the creation of technical content more efficient and elegant.

2025-11-07

How do variables such as `{id}`, `{filename}`, `{catname}`, `{module}` and others affect the display structure of the URL in static rules?

When operating a website, we often pay attention to the quality of the content and the performance of the website, but there is a detail that is often overlooked but is crucial, that is, the structure of the URL (Uniform Resource Locator).A clear and meaningful URL can not only enhance the user experience, but is also an indispensable part of search engine optimization (SEO).Our company, Anqi CMS, understands this well and provides flexible and powerful pseudo-static functions, allowing us to customize URLs that are both aesthetically pleasing and SEO-friendly according to our needs.

2025-11-07

How do the settings for image download, Webp format, large image compression, and thumbnail processing methods affect the display effect of the image in the content?

During the operation of a website, the presentation effect of images often directly affects user experience and the professionalism of content.Slow loading speed, inconsistent dimensions, format incompatibility, and other issues may cause visitors to leave.Fortunately, AnQiCMS provides us with a variety of powerful image processing features in the content settings, allowing us to manage images finely and create an excellent visual experience for website content.Next, let's delve deeper into how these features affect the display of website images. ### 1.

2025-11-07