In website operation, we often encounter such situations: the time information stored in the database, such as the publication time of articles, the creation time of products, etc., is often in the form of a string of numbers (timestamp).This sequence of numbers is clear to machines, but readability is far worse for us users.Imagine, an article displayed next to the date "1678886400" instead of "2023 March 15", doesn't it greatly reduce the experience?AnQi CMS knows this and has provided a very practical template tag -stampToDateHelp us easily format these original timestamps into beautiful and readable dates and times.

Why do we need to format timestamps?

The timestamp stored in the database is typically an integer representing the number of seconds or milliseconds since January 1, 1970, 00:00:00 UTC (Unix epoch).This unified storage method facilitates data processing and cross-time zone calculations, but displaying these numbers directly on the front-end page may confuse users and affect reading experience.Convert them to the format

Get to knowstampToDateTags and their usage

Of Security CMSstampToDateThe tag is designed to be very concise and efficient, it mainly requires two key pieces of information to complete the timestamp formatting:

  1. Original timestamp: This is the string of numbers you retrieved from the database. In AnQi CMS, this is usually a10 digitsinteger representing a second-level timestamp. For example,1609470335.
  2. Format stringThis is the template for defining how you want the date and time to be displayed. It may be used by some other systems.YYYY-MM-DDThis placeholder is different, Anqi CMS (developed based on Go language) uses the Go language's unique date and time formatting method. It uses a fixedreference date2006-01-02 15:04:05As a formatted "sample".

Please note, this reference date itselfwill notbe actually output. You need to match the components of the date and time you want with the reference date in the following.specific numberto write the format string. For example:

  • If you want to display a four-digit year, write2006.
  • If you want to display a two-digit month, write01.
  • If you want to display a two-digit date, write02.
  • If you want to display 24-hour time, write15.
  • If you want to display a two-digit minute number, write04.
  • If you want to display seconds with two digits, write05.
  • If you want to display the hour in 12-hour format, write3,and cooperatePMorAM.

stampToDateThe basic syntax structure of the tag is:

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

Actual operation and example

Let's look at some specific examples to see.stampToDateHow tags make timestamps 'alive'. Suppose we have an article objectitem, its published timestamp field isCreatedTime(with a value of1609470335as of January 1, 2021, 12:25:35 PM.

  1. Only show "Year-Month-Day":

    If you want to display a concise date in the article list, for example “2021-01-01”:“

    <p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
    

    The result will be:2021-01-01

  2. Display detailed “year-month-day hour:minute:second”:“

    If you need to display time information accurate to seconds, you can add references to hours, minutes, and seconds in the format string:

    <p>更新时间:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04:05") }}</p>
    

    The result will be:2021-01-01 12:25:35

  3. Customize Chinese or other delimiter formats:

    stampToDateThe strength lies in its flexibility. You can freely combine text and reference date numbers to create any format you want:

    <p>文章发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
    

    output:2021年01月01日

    <p>日期:{{ stampToDate(item.CreatedTime, "01/02/2006") }}</p>
    

    output:01/01/2021

    You can even use 12-hour clock and AM/PM markings:

    <p>具体时间:{{ stampToDate(item.CreatedTime, "3:04 PM") }}</p>
    

    output:12:25 PM(If it is afternoon, display PM, and in the morning, display AM)

  4. Common usage on the article detail page:

    On the article detail page, you usually have a current article object, for examplearchive. You can call its time field like this:

    <p>发表于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</p>
    <p>最后编辑:{{ stampToDate(archive.UpdatedTime, "2006-01-02 星期一") }}</p>
    

    The formatting rules of Go language also support more advanced usage, such as displaying the day of the week, month, etc.