In website operations, the time of content release or update is an important component of conveying information to users.A clear and readable date and time format, which can not only improve user experience but also enhance the professionalism and timeliness of the content.However, many content management systems usually store time in the background using a string of timestamps that are difficult to understand intuitively.prevArchiveThis tag retrieves the articleCreatedTimeorUpdatedTimeBy default, the original timestamp is returned

As an experienced website operations expert, I know how important it is to transform technical information into user-friendly displays.Today, let's delve into how to format these original timestamp data into the date and time format we are more accustomed to reading in AnQi CMS.

Understanding the original timestamp and user needs

Firstly, we need to understand why there is a situation where a series of numbers appears. Anqi CMS stores in the databaseCreatedTimeandUpdatedTimeThis time field uses the standard timestamp format, which is the number of seconds since 00:00:00 UTC on January 1, 1970 (Unix epoch). For example, you may see something similar in the template.1678886400Such numbers.Although this is very efficient and accurate for internal system processing, for website visitors, this string of numbers obviously has no meaning and cannot convey the key information of when the article was published or updated.

Our goal is toprevArchive/archiveDetailorarchiveListget tags fromitem.CreatedTimeorprev.UpdatedTimeTimestamps converted to readable formats such as 'March 15, 2023 10:30 AM' or '10:30 AM yesterday'.

The secret weapon of AnQi CMS:stampToDateTag

The AnQiCMS template engine (based on the powerful features of Go language) provides a very convenient built-in tag for this:stampToDate. This tag is used to format timestamps into the date-time string we need.

Its basic usage is:{{stampToDate(时间戳变量, "格式化字符串")}}.

The key here lies in the second parameter - 'format string'. Unlike many other languages, the Go language (as well as the AnqiCMS template engine) does not useY-m-d H:i:sInstead of such placeholders, use oneA specific reference timeBe used as a format template. This reference time is:2006年01月02日 15时04分05秒.

You are not mistaken, it is this string of numbers and words! You can use this reference time to 'mimic' it to the format you want the timestamp to be.

Let's look at a few examples:

  • If you want to display 'Year-Month-Day', for example2023-03-15Then the format string is"2006-01-02".
  • If you want to display 'Year/Year/Year/Month/Month/Day/Day', for example2023/03/15Then the format string is"2006/01/02".
  • If you want to display 'Year after year after year after year after year after year after year, every minute', for example2023-03-15 10:30Then the format string is"2006-01-02 15:04".
  • If you want to display 'Year after year after year after year after year after year after year, every minute and second', for example2023-03-15 10:30:00Then the format string is"2006-01-02 15:04:05".
  • If you want a Chinese format, for example2023年03月15日Then the format string is"2006年01月02日".

In this way, you can almost combine any date and time display format you want.

Practical application: Make time 'come alive' in the template

Now, we apply thisstampToDatelabel to a specific scenario

Scenario one: Format the time of a single article on the article detail page

Suppose you are designing a detailed article page and need to display the publication time and update time of the current article. Usually, the details of the current article are displayed through the template.archiveDirectly obtain the variable or througharchiveDetailLabel retrieval.

If you want to display the publication date as "Year-Year-Year-Month-Day-Day":

<p>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</p>

If you want to display the update time to the minute and include Chinese descriptions:

<p>最近更新:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15:04")}}</p>

This is a direct usearchive.CreatedTimeorarchive.UpdatedTimemethod, simple and efficient.

Scenario two: Formatting time in article lists or in the previous/next article

When you usearchiveListLoop through the article list, or useprevArchiveandnextArchiveTags can also be used to get the previous/next articlestampToDateTags can format time.

For example, in the display of the previous/next article:

{% prevArchive prev %}
  {% if prev %}
    <p>上一篇:<a href="{{prev.Link}}">{{prev.Title}} ({{stampToDate(prev.CreatedTime, "2006-01-02")}})</a></p>
  {% else %}
    <p>没有上一篇文章了</p>
  {% endif %}
{% endprevArchive %}

{% nextArchive next %}
  {% if next %}
    <p>下一篇:<a href="{{next.Link}}">{{next.Title}} ({{stampToDate(next.CreatedTime, "2006-01-02")}})</a></p>
  {% else %}
    <p>没有下一篇文章了</p>
  {% endif %}
{% endnextArchive %}

In the loop of the article list:

{% archiveList archives with type="list" limit="10" %}
  {% for item in archives %}
    <div>
      <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
      <p>发布于:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
      <p>{{item.Description}}</p>
    </div>
  {% endfor %}
{% endarchiveList %}

You can see, whether it is a single article detail or each item in the list,stampToDatethe label can flexibly convert the timestamp into the readable format we want.

optimize and **practice

In actual operation, date and time formatting is not just about technical implementation, but also about user perception.

  • Maintain consistency: The format of publication time and update time should be as consistent as possible throughout the website to avoid confusion for users.
  • Consider the contextFor news information websites, the publication time may need to be precise to the minute; while for long technical articles, it is sufficient to display to the day.Choose the appropriate format based on content type and user habits.
  • Utilize the flexibility of the Go language: The time reference format of Go language may look unique at first glance, but it is very flexible. You cantag-stampToDate.mdfind more detailed formatting examples in the documentation, and explore more possibilities.

Summary

Provided by Anqi CMSstampToDateLabel, we can easily convert the timestamp data from the backend into a user-friendly and easily understandable date and time format. Whether you are displaying the publish time of a single article or in an article