Converting 6 Jan 2021 21:19:42 GMT text into a date and time

Certified Lead Developer

What is the best way people have found to convert 6 Jan 2021 21:19:42 GMT text into date and time?

  Discussion posts and replies are publicly visible

  • f the format will always be consistent, you can create an expression rule to convert your string into a datetime.

    a!localVariables(
      local!months: {
        "Jan",
        "Feb",
        "Mar"
      },
      local!string: "6 Jan 2021 21:19:42 GMT",
      local!date: trim(left(local!string, 11)),
      local!time: totime(trim(right(local!string,12))),
      local!year: right(local!date,4),
      local!day: trim(left(local!date,2)),
      local!month: wherecontains(trim(mid(local!date,3,4)),local!months),
      datetime(local!year,local!month,local!day,hour(local!time),minute(local!time),second(local!time))
    )

  • Here is an alternative that uses regex if that's an option for you. The only reason I like this solution slightly better is that it can be modified to accept slightly different formats more easily. The downside is that I think regex will take slightly more time (very slightly more).

    Just posting for other ideas in the future if people need them.

    a!localVariables(
      local!userDateText: "6 Jan 2021 21:01:10 GMT",
      local!months: {
        "Jan": 1,
        "Feb": 2,
        "Mar": 3/*  Continue for all months  */
    
      },
      local!day: regexfirstmatch("^\d+", local!userDateText, "i"),
      local!month: index(
        local!months,
        regexfirstmatch("[a-zA-Z]{3}", local!userDateText),
        0
      ),
      local!year: regexfirstmatch("\d\d\d\d", local!userDateText, "i"),
      local!time: regexfirstmatch("\d+:\d+:\d+", local!userDateText, "i"),
      local!hour: split(local!time, ":")[1],
      local!min: split(local!time, ":")[2],
      local!sec: split(local!time, ":")[3],
      {
        datetime(
          local!year,
          local!month,
          local!day,
          local!hour,
          local!min,
          local!sec
        )
      }
    )