In website operation, the display of time information is everywhere, whether it is the current year in the copyright statement, the time of article publication, or the deadline of the event, dynamic and accurate time display is crucial for improving user experience and content timeliness.As an enterprise-level CMS dedicated to efficient content management, AnQiCMS (AnQiCMS) understands this need and therefore provides intuitive and powerful tags in the template system, allowing you to easily display the current year or a custom formatted time in website templates in real-time.
The template engine design of Anqi CMS draws inspiration from Django's syntax style, implementing various dynamic content output through concise tag syntax. For time display, we will mainly use two core tags:{% now %}and{{stampToDate()}}.
Elegantly display the current year:{% now %}tags
At the footer copyright statement area of the website, we often see text like 'Copyright © 2023 Your Company', where the year needs to be updated annually.Clearly modifying manually is not an efficient solution.{% now %}.
This tag can directly retrieve the current date and time of the server and output it according to the format you specify. If you just need to display the current year, you can simply write it like this in the template:
<footer>
© {% now "2006" %} {{ system.SiteName }} 版权所有。
</footer>
Here,"2006"Is a special date and time formatting string in Golang, representing the reference layout of the year. When the system parses to{% now "2006" %}When it is used, it will intelligently extract and replace the current year at this position. In this way, the copyright year of your website will be automatically updated every year without manual intervention, greatly improving operational efficiency.
Precise control of time format:{{stampToDate()}}标签的魔法English
if your needs are not just simple years, but more specific and richer date or time formats, such as displaying "October 27, 2023 14:35" on the article detail page, or if you need to convert timestamps stored in the database into readable date formats, then Anqi CMS provides more powerful features{{stampToDate()}}Label.
{{stampToDate()}}Label is a functional label that receives two main parameters: the first is the 10-digit Unix timestamp to be formatted, and the second is the string defining the output format. Its basic usage is as follows:
{{ stampToDate(时间戳, "格式化字符串") }}
For example, assume your article data objectarchivecontainsCreatedTimeThe field stores the timestamp of the article's creation, and you want to display it in the format of "year-month-day". You can do this:
<p>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>
The secret of Golang time formatting strings
It needs to be emphasized specially,"2006-01-02"This format string is not a random date, but a conventional Golang languageReference layoutIt indicates the position of time elements (year, month, day, hour, minute, second, etc.) in the string. Understanding this reference layout is essential for flexible application.stampToDateThe key is:
2006: Represents the year (Year)01: Represents the month (Month), e.g., January02: Represents the day (Day), e.g., the 2nd15: Represents hour (Hour), 24-hour format, e.g., 3 PM04: Represents minute (Minute)05: Represents second (Second)Mon/Monday: Abbreviation/Full name of the day of the weekJan/January: Abbreviation/Full name of the month
By combining these reference elements, you can build almost any date-time format you want.
The following are some common formatting examples:
- Display only the year:
{{ stampToDate(archive.CreatedTime, "2006") }}-2023 - Show Year-Month-Date:
{{ stampToDate(archive.CreatedTime, "2006-01-02") }}-2023-10-27 - Show Year/Month/Date:
{{ stampToDate(archive.CreatedTime, "2006/01/02") }}-2023/10/27 - Show Chinese Year-Month-Date:
{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}-2023年10月27日 - Show Time:Hour:Minute:Second:
{{ stampToDate(archive.CreatedTime, "15:04:05") }}-14:35:01 - Show Full Date and Time:
{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}-2023-10-27 14:35:01 - Show format with the day of the week:
{{ stampToDate(archive.CreatedTime, "Mon, 02 Jan 2006") }}-Fri, 27 Oct 2023
Through these flexible formatting options, you can precisely control the way time information is presented according to the design style and content needs of the website.
Actual application scenarios and combination usage
In AnQi CMS, the application scenarios of these tags are very extensive.
- Website footer:
<p>Copyright © {% now "2006" %} {{ system.SiteName }}. All Rights Reserved.</p> - Article or product list page:In
archiveListDisplay the publication date of each article when the tags cycle articles.{% archiveList archives with type="list" limit="10" %} {% for item in archives %} <div class="article-item"> <h3><a href="{{item.Link}}">{{item.Title}}</a></h3> <p>发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p> <p>{{item.Description}}</p> </div> {% endfor %} {% endarchiveList %} - Custom time display:If you have customized a field in the background to store a timestamp for an event (for example,
event.StartDate), you can also usestampToDate.<p>活动开始:{{ stampToDate(event.StartDate, "2006年01月02日 星期一 15:04") }}</p>
Whether it is a simple year annotation or a complex date and time formatting, Anqi CMS provides intuitive and powerful template tags to meet your needs. By using them flexibly,{% now %}and{{stampToDate()}}Tags, you can easily add timeliness and professionalism to the website content, thereby enhancing the overall user experience.
Common Questions (FAQ)
- Q: Why does the Golang time format string look like a specific date (e.g.,A: This is a unique feature of Golang's design. The time formatting in Golang is not defined through abstract placeholders, but by using a specific 'reference time' that is
2006年1月2日15点04分05秒 -0700 MSTcome as a layout example.The thing you need to do is write a string that matches the expected output format, containing the corresponding numbers or text parts from this reference time.The system will parse and format the actual time based on your layout.