In website content display, we often encounter time data obtained from the database as a long string of numbers, such as1678886400.This string of numbers, which is also commonly referred to as a timestamp, is very convenient for computers, but it seems cold and difficult to understand for users visiting websites.好在,SafeCMS为我们提供了一个非常便捷的功能,能够将这些原始的10位时间戳轻松转换为我们习惯阅读的日期和时间格式。

KnowstampToDateFormat labels

The template engine of Anqi CMS (which adopts the syntax similar to Django template engine) is built-in with a name ofstampToDateThe label, used specifically for handling timestamp formatting. Its usage is very intuitive, only requiring two parameters: the timestamp that needs to be formatted and10-digit timestampwhat you wantFormat string.

The basic syntax structure is as follows:

{{stampToDate(你的时间戳变量, "格式字符串")}}

What needs special attention here is the "format string". It is not what we are accustomed to in our daily lives.YYYY-MM-DDThis format, but adopts a special 'reference time' format unique to the Go language. Don't worry, it sounds a bit professional, but in fact it is very simple: you just need to remember2006-01-02 15:04:05This specific time is used as a standard template. By replacing the year, month, day, hour, minute, and second in this standard time, you can define any date and time display format you want.

For example:

  • If you want to displayYear-Month-DayFormat string as written"2006-01-02".
  • If you want to displayYear/Month/DayFormat string as written"2006/01/02".
  • If you want to displayMonth-day Time:minuteFormat string as written"01-02 15:04".
  • If you want to displayComplete YearMonthDayTimeSecondFormat string as written"2006-01-02 15:04:05".

Remember, this “2006-01-02 15:04:05”It does not represent the date you will get, it is just a “template”, the system will generate the final date and time based on the actual timestamp you provide and the corresponding position of this template.

Find your timestamp data in the template

In the templates of AnQi CMS, many time fields retrieved from the database are in the form of 10-digit timestamps by default. These fields usually haveTimesuffixes, such as:

  • The creation time of the article: archive.CreatedTime
  • The update time of the article: archive.UpdatedTime
  • The creation time of the comment: item.CreatedTime(When you are looping through the comment list)
  • and other similar onesCreatedTimeorUpdatedTimefield.

When you are usingarchiveDetail/archiveList/commentListWhen the tags are used to retrieve data, these timestamp fields will be included in the returned object, and can be accessed directly by.symbols.

Formatting timestamps into readable dates.

Now, let's takestampToDateLabel and the actual timestamp variable combined, see how to apply it in the template:

Suppose we are displaying the detail page of a document, and we need to show its publication time and update time:

<div class="article-meta">
    <p>
        发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 星期一")}}
    </p>
    <p>
        最近更新:{{stampToDate(archive.UpdatedTime, "2006/01/02 15:04:05")}}
    </p>
    <p>
        简短日期:{{stampToDate(archive.CreatedTime, "01-02")}}
    </p>
</div>

If we are iterating over a list of comments, and we want to display the posting time of each comment:

{% for comment in comments %}
    <div class="comment-item">
        <span class="comment-author">{{comment.UserName}}</span>
        <span class="comment-time">于 {{stampToDate(comment.CreatedTime, "2006年01月02日 15:04")}} 评论道:</span>
        <p class="comment-content">{{comment.Content}}</p>
    </div>
{% endfor %}

The English translation of 'auto' is 'English'.stampToDateTo format. This makes your website content look more professional and user-friendly.

PassstampToDateThis compact and powerful label, we can easily convert the cold timestamps in the database into user-friendly dates and times, greatly enhancing the reading experience of the website content.Mastering this technique, you can make every time information on the website come alive.


Common Questions (FAQ)

1. Why does my timestamp formatting show an error or blank?This usually has several reasons:

  • Invalid timestamp data:Check if the variable you enteredstampToDateis indeed a valid 10-digit timestamp. If the variable is empty, 0, or any non-numeric value, it may cause display errors. You can try using{{你的时间戳变量}}Direct output, confirm whether it is the expected number.
  • Format string error:The date format string of Go language is very strict, must use2006-01-02 15:04:05As a reference. Any minor error (such as writing the yearYYYY) will cause the formatting to fail. Please carefully check the reference format in the document.
  • Timestamp number of digits is incorrect: stampToDateLabel expects 10-digit (second-level) timestamp. If it is a 13-digit (millisecond-level) timestamp, you need to divide it by 1000 first.

2. Can I customize the format, such as displaying the day of the week or AM/PM?Of course. The date format string in Go language is very powerful, supporting many different formatting options, including days of the week, AM/PM, etc. For example:

  • Display the day of the week:"Mon Jan _2 15:04:05 2006"ofMonAbbreviation for days of the week,MondayThe full name of the day of the week.
  • Show AM/PM:"3:04PM"ofPMIt can show AM/PM. You can refer totag-stampToDate.mdMore examples provided in the document, try to combine the format you need.

3. Are all the time fields 10-digit timestamps? How do I know if a field is a timestamp?In AnQi CMS, it is usually named withTimesuffix fields (such asCreatedTime,UpdatedTime,LastLoginThe data is stored in the form of a 10-digit timestamp. If you are unsure about the specific data type of a field, you can try using the template.{{你的字段变量|dump}}This filter. It outputs the detailed type and value of the variable, helping you confirm if it is a timestamp.dumpThe result is displayed asint64or a similar integer type, and the number looks like a timestamp, then it can be usedstampToDate.