In website operation, we often encounter such situations: time information stored in the database, such as the publication time of articles, the creation time of products, etc., often exists in the form of a series of numbers (timestamp).This string of numbers is clear and easy for machines, but it has poor readability for us users.Imagine, if next to an article it shows "1678886400" instead of "2023年03月15日", wouldn't it be a big reduction in experience?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 usually an integer, representing the number of seconds or milliseconds since January 1, 1970, 00:00:00 UTC (Unix epoch).This unified storage method is convenient for data processing and cross-regional calculations, but directly displaying these numbers on the front-end page will confuse the user and affect the reading experience.Translate them to the format such as “2023/03/15 10:30” or “yesterday 14:00”, which can greatly enhance the professionalism and user-friendliness of the website.

KnowstampToDateTags and their usage methods

Anqi CMS'sstampToDateThe label is designed to be very simple and efficient, it mainly requires two key pieces of information to complete the timestamp formatting:

  1. Original timestampThis is the string of numbers you get from the database. In AnQi CMS, this is usually a10-digitinteger representing a timestamp in seconds. For example,1609470335.
  2. Format stringThis is the template defining how you want the date and time to be displayed. As used by some other systems,YYYY-MM-DDThis placeholder is different, the Anqi CMS (developed based on Go language) uses the Go language-specific date and time formatting method. It uses a fixedReference date2006-01-02 15:04:05as a formatted "sample".

Please note that this reference date itselfwill notbe output actually. You need to compare the date and time components you want with those in this reference date.Specific numberTo write a 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 the hour in 24-hour format, 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 number in 12-hour format, write3, and cooperate withPMorAM.

stampToDateThe basic syntax structure of tags is:

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

Actual operation and example

Let's look at several specific examples to seestampToDateTags are how we make timestamps 'come alive.' Suppose we have an article objectitem, its published timestamp field isCreatedTime(value is1609470335,i.e., January 1, 2021, 12:25:35).

  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 output 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 output will be:2021-01-01 12:25:35

  3. Customize Chinese or other separator 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 indicators:

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

    Output:12:25 PM(If it is in the afternoon, PM will be displayed, and AM in the morning)

  4. Common usage on the article detail page:

    In 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 the Go language also support more advanced usage, such as displaying the day of the week, month