Updated on 2026-05-12 GMT+08:00

Conversion Between Datetime Objects and Datetime Strings

  1. Processing functions
    • Intelligent conversion function dt_parse: converts a datetime string or Unix timestamp to a datetime object.
    • dt_astimezone: returns a datetime object with new time zone information.
  2. Scenario 1: converting a datetime string without time zone information to a datetime object in a specified time zone.

    For example, a datetime string like 2019-06-04 2:41:26 that lacks time zone information can be converted between different time zones by utilizing its Unix timestamp. In this case, convert a datetime from the Los Angeles time zone to the Shanghai time zone.

    • Raw log: The value of the time field is Los Angeles time.
      {
          "time" : "2019-06-04 2:41:26"
      }
    • Processing rule
      e_set("timestamp", dt_parsetimestamp(v("time"), tz="America/Los_Angeles"))
      e_set("Shanghai_time", dt_parse(v("timestamp"), tz="Asia/Shanghai"))
    • Processing result
      {
      	"Shanghai_time": "2019-06-04 17:41:26+08:00",
      	"time": "2019-06-04 2:41:26",
      	"timestamp": 1559641286
      }
  3. Scenario 2: converting a datetime string without a time zone to a datetime object with a time zone
    • Raw log
      {
          "time" : "2019-07-10 06:58:19"
      }
    • Processing rule
      e_set("new_time", dt_parse(v("time"), tz="Asia/Shanghai"))
    • Processing result
      {
      	"new_time": "2019-07-10 06:58:19+08:00",
      	"time": "2019-07-10 06:58:19"
      }
  4. Scenario 3: converting a datetime string with a time zone to a datetime object in the target time zone.
    • Raw log
      {
          "time" : "2019-06-04 2:41:26+08:00"
      }
    • Processing rule
      e_set("new_time",dt_astimezone(v("time"), "America/Los_Angeles"))
    • Processing result
      {
      	"new_time": "2019-06-03 11:41:26-07:00",
      	"time": "2019-06-04 2:41:26+08:00"
      }