How to dynamically display the current year or a specified date on a website?

Calendar 👁️ 92

In website operation, we often need to display some dynamic time information, such as the copyright year at the bottom of the website (such as “© 2023-Current Year”), the publication or update time of articles, or the deadline for specific activities, etc.These dynamic dates not only maintain the timeliness of the website information, but also improve the user experience, and in some cases, it also has a positive effect on SEO.

AnQi CMS provides a series of flexible template tags that allow you to easily display the current year or a specified date dynamically at any location on the website.We will delve into how to make use of these features in detail.

1. Dynamically display the current year of the website

The copyright information at the footer of the website usually needs to be automatically updated every year. If modified manually, it is麻烦 and easy to miss. Anqi CMS provides a very convenient tag for this:{% now "2006" %}.

This tag can directly obtain and display the current year of the website. It includes the"2006"It is not a specific year, but a special usage of date formatting in Go language, which represents the reference time2006年01月02日15时04分05秒The part of the year. Just place it in the template where the year needs to be displayed.

Example: Display the current year in the footer.

Assume you want to display copyright information like “© 2023 [Your company name] All rights reserved” in the footer of your website, and the year is automatically updated, you can do this in the template file (usuallyfooter.htmlOr include the footer code in the common template) as follows:

<p>© {% now "2006" %} {{ system.SiteName }} 版权所有。</p>

When the year 2024 comes, this label will automatically update to display “© 2024 [Your Company Name] All Rights Reserved”, saving you the trouble of manual modification.

Format the display of a specified date or timestamp

In addition to the current year, we also need to display the publication time, update time of the articles on the website, as well as the dates of specific events, etc. Anqi CMS provides{{ stampToDate(时间戳, "格式") }}This powerful tag to meet these needs.

Here时间戳It usually refers to a Unix timestamp (a number representing the number of seconds from 1970-01-01 00:00:00 to a specific time). The article publishing time in AnQi CMS (CreatedTime)、Update Time(UpdatedTime)are stored in the form of timestamps.格式Part determines the way dates are displayed.

Similarly,格式The parameter also follows the date formatting rules of the Go language, with2006-01-02 15:04:05This reference time is used as a template for matching. This means that if you want to display:

  • Year, use2006
  • Month, use01
  • Date, use02
  • Hour (24-hour format), use15
  • minutes, use04
  • seconds, use05

You can combine these elements as needed and add separators (such as year, month, day, slash, colon, etc.).

Example: Display the publication and update time of the article

In the article detail page (for examplearchive/detail.htmlor the corresponding detail template of the model), you can usearchiveDetailtags to get the article details, and then combinestampToDateto format the displayCreatedTimeandUpdatedTime:

{% archiveDetail archive with name="Title" %}
    <h1>{{ archive.Title }}</h1>
    <p>
        发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}
        {% if archive.UpdatedTime != archive.CreatedTime %} {# 如果更新时间与创建时间不同才显示 #}
        <span>(更新于:{{ stampToDate(archive.UpdatedTime, "2006-01-02") }})</span>
        {% endif %}
    </p>
    <div>
        {{ archive.Content|safe }}
    </div>

In this example:

  • "2006年01月02日 15:04"It will format the timestamp to the form 'October 26, 2023 10:30 AM'.
  • "2006-01-02"The timestamp will be formatted into a pure date form like '2023-10-26'.

Example: Display dates in the article list

If you want to display the article list page (for examplearchive/list.htmlOr the list template corresponding to the model) usually displays the publish date of each articlearchiveListLoop with tags:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article>
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
        <p>{{ item.Description }}</p>
        <a href="{{ item.Link }}">阅读更多</a>
    </article>
    {% endfor %}
{% endarchiveList %}

here,item.CreatedTimeIs the publish timestamp of each article in the loop, throughstampToDateLabel, we format it as年-月-日in the form of.

Chapter 3: Some application scenarios and tips

  • Multilingual date display:If your website supports multilingual, you can consider adjusting the language templates according to the habits of the target language.stampToDatelabel's格式Parameters, to adapt to the reading habits of users in different countries and regions.
  • Combine custom field:If you add a custom date field (such as "Event Start Date" or "Product Expiry Date") to an article or product model, as long as these fields store timestamps, they can also be used.stampToDateLabel formatting is displayed. For example, if you have a custom field namedevent_start_timeyou can call it like this in the template:{{ stampToDate(archive.event_start_time, "2006年1月2日") }}.
  • Dynamic year range:If you want to display a year range like '2020-current year', you can useCreatedTimeand the years of{% now "2006" %}:
    
    <p>© {{ stampToDate(archive.CreatedTime, "2006") }}-{% now "2006" %} 版权所有。</p>
    

The template tags of AnQi CMS provide great flexibility, making the management of website time information efficient and simple.Master the usage of these tags, and you will be able to better control the presentation of website content, providing visitors with more accurate and timely information.


Frequently Asked Questions (FAQ)

1. Why does my date formatting come out differently than expected, instead showing a number combination like '2006-01-02'?

This is because Go language (the development language of AnQi CMS) uses a fixed 'reference time' as a template for date formatting, rather than like some other languagesY-m-dor%Y-%m-%dAn equal sign. This reference time is2006年01月02日15时04分05秒. You must use the corresponding number in this reference time to represent the year, month, day, hour, minute, and second you want. For example, if you want to display the year, you should use"2006"If you want to display the month, use"01"Just build the format string according to the numbers in the reference time to get the expected result.

How to display a year range similar to '2020-current year'?

You can usestampToDateGet the starting year of the tag and combine it with{% now "2006" %}Label to get the current year. For example, if the content is published in 2020 and you want to display '2020-Current Year':

<p>版权信息:© {{ stampToDate(archive.CreatedTime, "2006") }}-{% now "2006" %}</p>

This will be extracted firstarchive.CreatedTimeThe year (e.g., 2020), then connect a hyphen, and finally display the current year (e.g., 2024).

3. Can I dynamically display the date in other locations on the website besides the footer?

Of course you can.The AnqiCMS template tags can be used at any position in the website template.

  • Website Header:For example, display the latest dynamic release date of the website.
  • Sidebar:Display the time of the latest comments or the date of an upcoming event.
  • Custom content block:Add an independent date information module on the homepage or other pages. *

Related articles

How to use the template inheritance mechanism to unify and display the website page structure?

When building a website, maintaining consistent page structure, improving development efficiency, and reducing maintenance costs are key to website operations.AnQiCMS (AnQiCMS) leverages its powerful functionality based on the Django template engine syntax to provide us with an efficient template inheritance mechanism, making it easy to unify and flexibly display the structure of web pages. ### The core concept of template inheritance: building the skeleton of a website Imagine that your website is like a house made up of different rooms. Each room (page) has its unique functions and content, but they share the same foundation

2025-11-08

How to reference and display common code snippets (such as header, footer) in AnQiCMS templates?

It is crucial to maintain consistency in page structure and code maintainability in website construction and content management.For a content management system like AnQiCMS, effectively referencing and managing common code snippets, such as the website header (Header) and footer (Footer), is the key to achieving this goal.Through AnQiCMS's flexible template mechanism, even users with little understanding of technical details can easily implement these features, thus building a website with clear structure and easy maintenance.###

2025-11-08

How to define and display temporary variables in the template?

In Anqi CMS template development, mastering how to define and display temporary variables in the template is a very practical skill.It can help us handle data more flexibly, optimize template structures, and write more concise and efficient code.Temporary variables are like the "small tags" you grab on the fly while making a page, used to temporarily store some data for later use.### Why do we need temporary variables? Imagine a scenario: you might need to get a value from a label and then perform a series of operations on it (such as truncating strings, formatting time)

2025-11-08

How to conditionally display different content or styles within a loop?

In website content display, we often encounter the need to display different content or assign unique styles to elements according to the order of data cycles, specific attributes, or states.For example, odd and even rows in the list need different background colors, special markings for certain types of products, or when a field is empty, hide the corresponding area.With its flexible and powerful Django-like template engine, AnQi CMS makes these requirements simple and easy to implement.By cleverly combining loop tags (`{% for ... %}`) and conditional judgment tags (`{% if ... %}`)

2025-11-08

How does AnQi CMS display or hide specific content based on user group permissions?

In daily website operation, we often encounter such needs: some content is intended for everyone to see, while some is only open to specific members, or shows different information for users of different levels.AnQiCMS (AnQiCMS) provides powerful user group permission management features, allowing us to flexibly control the visibility of website content and achieve the 'thousand faces of content'. ### Why do you need to display or hide content by user group?Imagine you are running a website that offers a variety of services, including free basic articles and paid in-depth reports

2025-11-08

How to display the document list matching the keywords on the search results page?

When visitors come to your website and hope to quickly find the information they need, a highly efficient and accurate on-site search feature is particularly important.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, providing you with a flexible and powerful content management and template system, allowing you to easily display a list of documents matching the search keywords on the search results page.Next, let's see how to implement this function together.### Learn about AnQi CMS search mechanism AnQi CMS has built-in powerful search capabilities, it will automatically index the content you publish

2025-11-08

How to prevent content collection interference code and watermark display in AnQi CMS?

Today, with the increasing richness of digital content, the value of original content becomes more and more prominent, but the problems of content plagiarism and collection that come with it also make many content creators and operators headache.AnQiCMS (AnQiCMS) fully understands this pain point, and therefore built-in strong content protection mechanisms from the very beginning of the system design, including anti-crawling interference codes and image watermark functions.These features are designed to help users effectively defend original copyright, ensure content exclusivity, and thereby enhance content value.

2025-11-08

How to dynamically display the canonical URL corresponding to the current page in the template?

In website operation, maintaining search engine friendliness is one of the key links to improving website visibility.Among them, the 'Canonical URL' plays a crucial role.It acts like a compass for search engines, telling them which page is the main version of the content, thus effectively avoiding ranking fragmentation due to content duplication or similarity.For content operators, correctly setting and displaying standard links is the foundation for ensuring that website content is efficiently crawled and indexed by search engines

2025-11-08