Skip to main content

I am trying to subtract old UTC time with current UTC time. I am getting result in period format. But I need in UTC Format. Can anyone please help me in converting period to UTC time format.

 

Dataweave script:

%dw 2.0

output application/json

var oldTym = "04:44:09.666829Z"

---

{

"TymDiff": now() as Time - oldTym

}

 

Result;

{

"TymDiff": "PT1M24.114088S"

}

 

"PT1M24.114088S" I need this in UTC format(as oldTym variable).

2 respuestas
  1. 20 mar 2021, 18:01

    Hi!

     

    it is important to understand that when you subtract two dates what you end up is not a Date, it is a Duration. Vishal above is using the fact that you can deconstruct a Duration in DataWeave per the docs here: https://docs.mulesoft.com/mule-runtime/4.3/dataweave-types#dw_type_dates

     

    My only concern with Vishal's script above is that he is adding "Z" to the end of the string. This would imply that somehow the duration is a "Zulu" duration, which doesn't make any sense. A duration is the same no matter where it occurs, the time zone. A traditional duration format would generally also pad the seconds to two characters, so I would tend to use this slightly-modified version:

     

    Hi! it is important to understand that when you subtract two dates what you end up is not a Date, it is a Duration.dw2Do note that this is going to require some additional tweaking if you intend to subtract two dates with more than 24 hours between them!