Skip to main content
January 20, 2023
Question

Time casting

  • January 20, 2023
  • 1 reply
  • 0 views

Hi Experts,

I have a requirement where we have to get the input from user for the time fields, User want the time to be entered in HH:MM:SS in 24hrs format where SS is optional.

Below are the input with expected stored output

Input1: 23:55:55      Output: 23:55:55

Input2: 10:32           Output: 10:32:00

Input3: 23                Output  23:00:00

Input4: 231000        Output: 23:10:00

Input5: 2359            Output: 23:59:00

Input6: 23               Output: 23:00:00

Kindly suggest for the feasibility of such expression as how can we achieve this.

1 reply

csteward
January 20, 2023

You can accomplish this with a combination of a few different text functions.  There are more considerations if you cannot restrict the input to an integer (or 3 integer fields), but something like this should get you what you need.  This is designed as a rule with ri!timeIn as Text type.

Note this is a basic setup by just stripping all text such as the colons ":" by converting tointeger(), as such it will also allow any text between the numbers such as "23::::45-22", if the format restriction is a bigger concern we will have to apply additional validation on it (e.g., this works but is 'loose').

if(
  rule!APN_isBlank(tointeger(ri!timeIn)),
  null,
  a!localVariables(
    local!formatted: substitute(
      padright(
        tointeger(
          ri!timeIn
        ),
        6
      ),
      " ",
      0
    ),
    
    concat(
      if(
        left(local!formatted,2)>23,
        "00",
        left(local!formatted,2)
      ),
      ":",
      if(
        mid(local!formatted,3,2)>59,
        "00",
        mid(local!formatted,3,2)
      ),
      ":",
      if(
        right(local!formatted,2)>59,
        "00",
        right(local!formatted,2)
      ),
    )
  )
)