Updated on 2025-01-10 GMT+08:00

Converting a Timestamp into a Date

Function Name

$timestamp_format(String A, String B)

Parameter Description

  • String A: timestamp to be converted. The value is a numeric string containing a maximum of 20 digits. You can also use the built-in function $timestamp() to obtain the current timestamp.
  • String B: date and time value, consisting of the year, month, day, hour, minute, second, and millisecond. Where,
    • Year: represented by letter "y" and consists of 4 characters.
    • Month: represented by letter "M" and consists of 1 to 2 characters.
    • Day: represented by letter "d" and consists of 1 to 2 characters.
    • Hour: represented by letter "H" and consists of 0 to 2 characters.
    • Minute: represented by letter "m" and consists of 0 to 2 characters.
    • Second: represented by letter "s" and consists of 0 to 2 characters.
    • Millisecond: represented by letter "S" and consists of 3 characters.
  • Table 1 Letters and corresponding parameter types

    Letter

    Date/Time Element

    Type

    Example

    G

    Era identifier

    Text

    AD

    y

    Year

    Year

    1996, 96

    M

    Month in a year

    Month

    July, Jul, 07

    w

    Week number in a year

    Number

    27

    W

    Week number in a month

    Number

    2

    D

    Day number in a year

    Number

    189

    d

    Day number in a month

    Number

    10

    F

    Week number in a month

    Number

    2

    E

    Day in a week

    Text

    Tuesday, Tue

    a

    am/pm flag

    Text

    PM

    H

    Hour number in a day (0–23)

    Number

    0

    k

    Hour number in a day (1–24)

    Number

    24

    K

    Hour number in am/pm (0–11)

    Number

    0

    h

    Hour number in am/pm (1–12)

    Number

    12

    m

    Minute number in an hour

    Number

    30

    s

    Second number in a minute

    Number

    55

    S

    Millisecond

    Number

    978

    z

    Time zone abbreviation

    Text

    PST, EST

    Z

    Time zone offset

    Text

    +800, -0530

    1. Each letter has its own meaning and is case sensitive.
    2. If one of "H", "m", "s" is 0, the other two must also be 0.

    A date and time value can contain spaces, hyphens (-), slashes (/), and colons (:), but not escape characters, such as \n. Common formats include but are not limited to the following:

    • yyyy-MM-dd HH:mm:ss
    • yyyyMMddHHmmss
    • yyyyMMddHHmmssSSS
    • yyyy-M-d H:m:s
    • MM-dd-yyyy HH:mm:ss
    • MM/dd/yyyy HH/mm/ss
    • MM/d/yyyy H/mm/ss
    • MM/d/yyyy H/mm/ss SSS
    • yyyyMMdd SSS
    • yyyy-MM-dd HH:mm:ss SSS
    • yyyy-MM-dd HH:mm:ss
    • yyyy-MM-dd HH:mm
    • yyyy-MM-dd HH
    • yyyy-MM-dd
    • yyyy-MM
    • yyyy
    • yy
    • MM-dd HH
    • MM-dd
    • MM
    • dd
    • HH:mm:ss SSS
    • HH:mm:ss
    • HH:mm
    • HH
    • mm
    • mm:ss
    • ss
    • SSS

Function Description

Converts a timestamp into a date in the corresponding format. The timestamp is the total number of milliseconds from 1970-01-01 00:00:00 to the specified time.

Parameter A in the function supports the following types:

  • Date and time in the format listed in Parameter Description
  • Environment parameters
  • Local parameters
  • Other built-in functions

Parameter B in the function supports the following types:

  • Date and time in the format listed in Parameter Description
  • Environment parameters
  • Local parameters
  • Other built-in functions

Application Scenarios

The timestamp-to-date function can be used in the following scenarios for API automation:

  • Request URL
  • Request header
  • Request body
  • Checkpoint property
  • if condition
  • for loop interrupt condition

Example

  • Request URL

    As shown in the following figure, the value of date in the request URL is the timestamp-to-date conversion function. Parameter A in the function is the built-in function $timestamp() for obtaining the current timestamp. For details, see Obtaining the Current Timestamp. Parameter B is yyyy-MM-dd HH:mm:ss.

  • Request header

    As shown in the following figure, the value of date in the request header is the timestamp-to-date conversion function. Parameter A in the function is the environment parameter date, and parameter B is yyyyMMddHHmmss. For details about how to set environment parameters, see Setting Environment Parameters of an API Script.

  • Request body

    As shown in the following figure, the request body uses the timestamp-to-date conversion function. Parameter A in the function is 123456789, and parameter B is yyyyMMddHHmmssSSS.

  • Checkpoint property

    As shown in the following figure, the target value of the checkpoint property result is the timestamp-to-date function. Parameter A in the function is the built-in function $timestamp() for obtaining the current timestamp, and parameter B is MM/dd/yyyy HH/mm/ss.

  • if condition

    As shown in the following figure, the target value of the if condition is the timestamp-to-date function. Parameter A in the function is the built-in function $timestamp() for obtaining the current timestamp, and parameter B is MM/d/yyyy H/mm/ss SSS.

  • for loop interrupt condition

    As shown in the following figure, the target value of the for loop interrupt condition is the timestamp-to-date function. Parameter A in the function is the built-in function $timestamp() for obtaining the current timestamp, and parameter B is yyyyMMdd SSS.