Updated on 2025-12-04 GMT+08:00

Conversion Between Datetime Strings and Unix Timestamps

  1. Processing functions
    • Intelligent conversion function: dt_str, which converts a Unix timestamp, datetime object, or datetime string to a datetime string in a specified format.
    • dt_strftimestamp, which converts only a Unix timestamp to a datetime string.
    • Intelligent conversion function: dt_parsetimestamp, which converts a datetime string or datetime object to a Unix timestamp.
  2. Scenario 1: Convert a datetime string without time zone to a Unix timestamp.

    For a datetime string without time zone information, for example, 2019-06-02 18:41:26, you need to specify the time zone when converting the date and time to a Unix timestamp. The Unix timestamp varies according to the time zone.

    • Raw log
      {
          "time": "2019-07-10 14:58:19"
      }
    • Processing rule
      e_set("Shanghai_timestamp", dt_parsetimestamp(v("time"), tz="Asia/Shanghai"))
      e_set("Los_Angeles_timestamp", dt_parsetimestamp(v("time"), tz="America/Los_Angeles"))
      e_set("UTC_timestamp", dt_parsetimestamp(v("time")))
    • Processing result
      {
      "time": "2019-07-10 14:58:19",
      "Shanghai_timestamp": 1562741899,
      "Los_Angeles_timestamp": 1562795899,
      "UTC_timestamp": 1562770699
      }
  3. Scenario 2: Convert a user-defined non-standard date format without time zone to a Unix timestamp.
    • Raw logs
      {
          "time1" : "2019-07-10 06:58:19",
          "time2": "2019/07/10 06-58-19"
      }
    • Processing rule
      e_set("time3", dt_parsetimestamp(v("time1")))   
      e_set("time4", dt_parsetimestamp(dt_strptime(v("time2"), "%Y/%m/%d %H-%M-%S")))
    • Processing result
      {
      	"time1": "2019-07-10 06:58:19",
      	"time2": "2019/07/10 06-58-19",
      	"time3": 1562741899,
      	"time4": 1562741899
      }