As an experienced website operations expert, I am well aware that how to flexibly and accurately display time information in a content management system is crucial for the vitality of website content and user experience.AnQiCMS (AnQiCMS) has fully considered this point in the design of template tags and provided multiple methods for handling time.nowTags andstampToDateLabel, analyze the main differences in the display of time.

nowLabel: Capture the snapshot of the moment.

Imagine that you need the copyright year at the bottom of the website to always be up-to-date, or you want to display a real-time "current time" prompt on the page. At this point,nowThe tag is your ideal choice. Its design purpose is very pure: to obtainThe current system time of the serverand output it in the format you specify.

nowThe usage of the tag is concise and clear, for example{% now "2006" %}It can be directly displayed on the page to show the current year. If you want to display the full date and time, you can write{% now "2006-01-02 15:04:05" %}.The strength of this label lies in its 'immediacy' - it does not rely on any external time data input, but directly retrieves the current time point from the server.nowThe label can always accurately display the latest time at that moment.It is very convenient for displaying the copyright year of the website, the real-time dynamic update of the date identifier, or any time information that needs to reflect the "moment".

stampToDateTag: A painting of historical moments

withnowThe "immediacy" of tags is different,stampToDateThe label is more concerned with the "reproduction of history.In website content, we often need to display the publication time of articles, product update dates, or event deadlines, etc., these time points are already determined at the time of content creation or the occurrence of a specific event.They are stored as 'historical timestamps' in the database, not the current real-time time.

stampToDateThe core responsibility of tags is to represent an originaltimestamp (usually 10 digits)Presented elegantly according to the format you set. For example, in AnQi CMS, the publication time of a document may be stored asarchive.CreatedTimeSuch a timestamp field. You would use to display it in a readable date format{{stampToDate(archive.CreatedTime, "2006-01-02")}}. Here,archive.CreatedTimeThat is the timestamp passed in, and"2006-01-02"It is the format you wish to display. Whether it is the publish and update date on the article detail page, or the timestamp of user comments,stampToDateCan transform these static numbers into date strings with clear meanings, allowing readers to clearly understand the time when the event occurred.

The core difference: The source and processing of time information

Then, where is the main difference between these two tags that seem to be related to time formatting? In simple terms, the difference lies in how they obtain time information."Source".

nowThe label is a 'time provider', it directly asks the server 'What time is it now?'}]Please then format and output the answer.It does not care about any other time data, only focusing on the current system time.This is like, you glance at a calendar or clock and know the date and time right now.

AndstampToDateA label is a 'time processor'. It does not generate time information itself, but requires youto actively provide an existing timestamp.Its task is to 'translate' this timestamp into a human-readable date and time format.This is like picking up an old photograph that bears the date and time of a moment, and then deciding what kind of handwriting to copy it down in.The date on this photo will not change because of the moment you pick it up, it is a fixed historical record.

Therefore, when you need to display the "current" or "real-time" time, nowLabels are your preferred choice. When you need to format specific timestamps from databases or other places,stampToDateTags are the correct tools. Both take advantage of the flexible time formatting strings in Golang, which keeps them highly consistent and convenient in custom output.

How to choose the appropriate tag?

Understanding this difference at the source makes it clear and easy to choose.

  • UsenowTags:When you want to display dynamic real-time information on the website, such as the real-time copyright year in the footer, the latest generation time of the website, or any time point that needs to reflect the "current" moment when the page is loaded.
  • UsestampToDateTags:When you need to format the display of fixed timestamp data stored in content such as articles, products, user comments, etc., such as article publishing/update time, user registration time, event start/end dates, and so on.

These two tags are similar in purpose, but there are essential differences in the source and processing logic of time information. Mastering their correct usage will greatly enhance your efficiency and flexibility in content operations on AnQi CMS.


Frequently Asked Questions (FAQ)

  1. Question:stampToDateIs the first parameter of the label supposed to be a 10-digit timestamp? Do I use the date string directly?Answer: Yes,stampToDateThe first parameter of the labelMust be a 10-digit timestamp(Unix timestamp), it represents the number of seconds elapsed since 00:00:00 UTC on January 1, 1970. If you have a date string (such as "2023-01-01 10:30:00"), you cannot use it directly tostampToDateFormat.In this case, you may need to handle the conversion of date strings to timestamps on the backend, or check if AnQi CMS has other tags or filters that support date string formatting.

  2. Question:nowCan the tag be used to display the publication time of the article? This way, every time the article is visited, the time is the most up-to-date.Answer:nowThe tag is not suitable for displaying the publication time of the article becausenowThe label shows the time of website visitCurrent system time, while the publication time of the article is a fixed historical record, which will not change with user access. If usingnowDisplaying the publish time would make all articles look like they were "just published", which is obviously not in line with the actual situation. You should usestampToDateCombine the article data's publish timestamp field (for example{{stampToDate(archive.CreatedTime, "2006-01-02")}}) to correctly display the article's publish time.

  3. Ask: If I only want to get the number of the current month (for example, '1' represents January), how should I usenowOrstampToDate? What format string should I write?Answer: You should usenowThe tag, because it retrieves the current time. To display the number of the current month, you can use the Golang formatting string"01"So, the tag will be{% now "01" %}. If you need to display the full two-digit month (e.g., "01"), use"01"; If you only need to display a single digit (e.g., "1"), use"_1".