How to get working days between 2 time stamps

Certified Senior Developer

I am calculating time difference between 2 time stamps in   hours: min :sec   format  .

Now my problem is I have to eliminate weekends between 2 time stamps and calculate difference in   hours: min :sec   format  

  Discussion posts and replies are publicly visible

Parents Reply Children
  • 0
    Certified Senior Developer
    in reply to likhithan

    Here is a reference where you can check the conversions. and create a custom expression rule to evaluate accordingly.

    Epoch Converter

  • 0
    Certified Senior Developer
    in reply to Konduru Chaitanya

    a!localVariables(
      local!MaxDateTimeForAssigned: todatetime(
        rule!ERR_GetMaxDateTimeByStatus(
          statusId: cons!ERR_STATUS[3],
          trackingId: ri!trackingId
        )
      ),
      local!MaxDateTimeForWaitingApproval: todatetime(
        rule!ERR_GetMaxDateTimeByStatus(
          statusId: cons!ERR_STATUS[5],
          trackingId: ri!trackingId
        )
      ),
      local!CycleTime: if(
        or(
          a!isNullOrEmpty(local!MaxDateTimeForAssigned),
          a!isNullOrEmpty(local!MaxDateTimeForWaitingApproval)
        ),
        {},
        local!MaxDateTimeForWaitingApproval - local!MaxDateTimeForAssigned,
        
      ),
      local!format: if(
        a!isNullOrEmpty(local!CycleTime),
        {},
        split(local!CycleTime, ".")[1],
        
      ),
      local!days: if(
        a!isNullOrEmpty(local!format),
        {},
        split(local!format, "::")[1],
        
      ),
      local!hours: if(
        a!isNullOrEmpty(local!format),
        {},
        if(
          local!days = 0,
          split(local!format, ":")[3],
          sum(
            (local!days * 24),
            split(local!format, ":")[3]
          )
        )
      ),
      local!min: if(
        a!isNullOrEmpty(local!format),
        {},
        split(local!format, ":")[4]
      ),
      local!sec: if(
        a!isNullOrEmpty(local!format),
        {},
        split(local!format, ":")[5]
      ),
      local!TotalCycleTime: {
        if(
          and(
            a!isNullOrEmpty(local!hours),
            a!isNullOrEmpty(local!min),
            a!isNullOrEmpty(local!sec)
          ),
          {},
          local!hours & ":" & local!min & ":" & local!sec
        )
      },
      local!TotalCycleTime
    )

    This will not be coming in hours:min: sec format . i already have rule where I can get difference between time stamps but my only problem is to eliminate weekend and get in the hours: min: sec format as output

  • 0
    Certified Senior Developer
    in reply to likhithan

    Yes. You will have to construct your expression rule to calculate the difference based on the below values


    Also I would say try out these for better understanding

    I can understand the above mentioned way is a complex one. But the conversion of your date times will give you accurate values.

  • 0
    Certified Senior Developer
    in reply to Konduru Chaitanya

    Can you please check my code. I have sent . The way you are saying like how can I eliminate weekends there

  • 0
    Certified Senior Developer
    in reply to likhithan

    i think then you need to calculate works hours initially 

  • 0
    Certified Senior Developer
    in reply to Abhishek Karumuru

    If I calculate initially then I will be getting 1 day with the dates I mentioned in the output. The the output cannot be 1 day because time difference in only 2 minutes

  • 0
    Certified Senior Developer
    in reply to Konduru Chaitanya

      any suggestion here after seeing my code

  • 0
    Certified Senior Developer
    in reply to likhithan

    a!localVariables(
      local!date1: (
        "Your date time 1 here" - todatetime("01/01/1970")
      ) * 24 * 60 * 60,
      local!date2: (
        "Your date time 2 here" - todatetime("01/01/1970")
      ) * 24 * 60 * 60,
      local!getEpochfor1: index(split(local!date1, ":"), 1, null),
      local!getEpochfor2: index(split(local!date2, ":"), 1, null),
      local!difference: local!getEpochfor2 - local!getEpochfor1,
      local!days: rounddown(local!difference / 86400, 0),
      local!hours: rounddown(mod(local!difference / 3600, 24), 0),
      local!minutes: rounddown(mod(local!difference / 60, 60), 0),
      local!sec: rounddown(mod(local!difference, 60), 0),
      concat(
        local!days,
        " Days",
        " ",
        local!hours,
        " Hours",
        " ",
        local!minutes,
        " Mins",
        " ",
        local!sec,
        " Secs"
      )
    )

    Try replacing your date time values in the local!date1 and local!date2. Not sure how accurate it works for all values. But as far as I am getting for different values, it is giving me the expected answer

  • 0
    Certified Senior Developer
    in reply to Konduru Chaitanya

    Sure I will check once. Thanks. Did your code remove weekends here

  • 0
    Certified Senior Developer
    in reply to Konduru Chaitanya

    a!localVariables(
      local!date1: datetime(2024, 4, 11, 13, 24, 41, 0),
      local!date2: datetime(2024, 4, 15, 13, 24, 41, 0),
      local!getEpochfor1: index(split(local!date1, ":"), 1, null),
      local!getEpochfor2: index(split(local!date2, ":"), 1, null),
      local!difference: local!getEpochfor2 - local!getEpochfor1,
      local!days: rounddown(local!difference / 86400, 0),
      local!hours: rounddown(mod(local!difference / 3600, 24), 0),
      local!minutes: rounddown(mod(local!difference / 60, 60), 0),
      local!sec: rounddown(mod(local!difference, 60), 0),
      concat(
        local!days,
        " Days",
        " ",
        local!hours,
        " Hours",
        " ",
        local!minutes,
        " Mins",
        " ",
        local!sec,
        " Secs"
      )
    )