Skip to main content
New Participant
Feb 5, 2023 3:22 PM UTC
Question

How can we get difference between two times ?

Hi All,

I am having two times saves in 2 variables 

Eg: 1)  2/5/2023 9:00 AM GMT+05:30.  2)   2/5/2023 8:00 PM GMT+05:30

now my ask is what function we should use in order to get the output like output : 11 hrs or in minuts

and also same for dates 

Thank you 

12 replies

yanaln6436
Inspiring
Feb 5, 2023 4:22 PM UTC

After.. decrypting.. your question, I understood that you want to find the difference in time between 2 date/dateTime values, here you go mate; this should help:

a!localVariables(
  local!firstDateTime: now(),
  local!secondDateTime: subtractminutes(subtracthours(now(), 4), 15),
  local!timeDifference: elapsedtime(
    local!secondDateTime,
    local!firstDateTime
  ),
  a!map(
    mintuesDifference: local!timeDifference[5],
    hoursDifference: local!timeDifference[4],
    dateDifference: elapseddays(
      local!secondDateTime,
      local!firstDateTime
    ),
    hoursWithMinutes: time(
      local!timeDifference[4],
      local!timeDifference[5]
    )
  )
)

also consider checking these sections for later requirements on date manipulations. 

New Participant
Feb 5, 2023 4:32 PM UTC

Thank you for the answer elapseddays() function is not avaliable 

yanaln6436
Inspiring
Feb 6, 2023 8:38 AM UTC

elapsedtime()... my locals' names are self-explanatory, its right there mate, I recommend reading the function description to fully understand it. 

Participating Frequently
Jul 19, 2023 11:25 AM UTC

Thanks [mention:8419a5a2e69f4f048b1b155b9fc7b8f3:e9ed411860ed4f2ba0265705b8793d05]

mikes0011
Brainy
Jul 19, 2023 1:09 PM UTC

Generally you can just subtract a datetime value from another, it just takes some effort to meaningfully unpack the result.  Here's my quick attempt at something simple:

a!localVariables(
  
  local!time1: datetime(2023, 02, 05, 09, 04),  
  local!time2: datetime(2023, 02, 05, 20, 02),
  
  local!difference: local!time2 - local!time1,
  
  a!map(
    days: tointeger(local!difference),
    hours: hour(local!difference),
    minutes: minute(local!difference),
  )
)

The Expression Guru