Manipulate Date Time Operations In Dataweave

 

This tutorial talks about how to manipulate different Date time operations in Dataweave, it include various Dataweave date time functions to perform date time conversions in different formats.

After this tutorial you will be able to perform different date time operations

 

Convert Current Datetime to CST Timezone

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as DateTime >> 'America/Chicago'

Result:

2020-07-19T09:56:14.020-05:00[America/Chicago]

 

Convert Current DateTime to specific string format(yyyy-MM-dd)

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as DateTime as String {format: "yyyy-MM-dd"}

Result:

2020-07-19

 

Convert Current DateTime to specific string format(yyyy-MMM-dd)

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as DateTime as String {format: "yyyy-MMM-dd"}

Result:

2020-Jul-19

 

Convert Current DateTime to specific string format( AM/PM)

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as DateTime as String {format : "d-MM-yyyy h:mm:ss a"}

Result:

19-07-2020 9:02:48 PM

 

Fetch Current Date In dataweave

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as Date

Result:

19-07-2020

 

Fetch Current Time In dataweave

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as Time

Result:

20:41:12.170+05:30

 

Format Time In dataweave(HH:MM:SS)

 

Dateweave Expression:

%dw 2.0
output application/java
---
now() as String {format: "hh:m:s"}

Result:

08:46:28

 

Fetch Year From Current Date in dataweave

 

Dateweave Expression:

%dw 2.0
output application/java
---
now().year

Result:

2020

 

Fetch current day value from current Date in dateweave

 

Dateweave Expression:

%dw 2.0
output application/java
---
now().day

Result:

19

 

Fetch month value from Current Date in dataweave

 

Dateweave Expression:

%dw 2.0
output application/java
---
now().month

Result:

7

Add,Substract Date and Time in dataweave

In this part we will see few examples where we will add and substract the day and time and use P as period date type

 

Get 2 Day prior Date

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() - |P2D|

Result:

2020-07-20T13:27:16.731+05:30[Asia/Calcutta]

 

Get 2 Day prior Date from Custom Date Format

 

Dataweave Expression:

%dw 2.0
output application/java
---
"2020-02-01" - |P2D|

Result:

2020-01-30

 

Add No of Days to Current Date

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() + |P2D|

Result:

2020-07-24T13:34:23.548+05:30[Asia/Calcutta]

 

Add No of Days to Custom Date

 

Dataweave Expression:

%dw 2.0
output application/java
---
"2020-02-01" + |P2D|

Result:

2020-02-03

 

Add No of Years to Current Date

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() + |P2Y|

Result:

2022-07-22T13:36:45.855+05:30[Asia/Calcutta]

 

Add No of Years to Custom Formatted Date

 

Dataweave Expression:

%dw 2.0
output application/java
---
"2020-02-01" + |P2Y|

Result:

2022-02-01

 

Substract No of Years From Current Date

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() - |P10Y|

Result:

2010-07-22T13:39:52.551+05:30[Asia/Calcutta]

 

 

Add Hours,Minute,Time In Current Time: we are adding 2 Hours,13 Minute and 9 Seconds, you can also substart the same using – symbol

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() as String {format: "hh:m:s"} + |PT2H13M9S|

Result:

03:59:03Z

 

How to substract specific year with specific date and time from single dataweave expression

 

Dataweave Expression:

%dw 2.0
output application/java
---
|2020-11-02T23:57:59| - |P2Y9M1D| - |PT50M510S| + |PT3H|

Result:

2018-02-02T01:59:29

 

Compare 2 Dates In Dataweave

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() as DateTime as String {format: "yyyy-MM-dd"} < "2020-09-10"

Result:

True

 

Dataweave Expression:

%dw 2.0
output application/java
---
now() as DateTime as String {format: "yyyy-MM-dd"} < "2020-09-10"

Result:

false

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Andee
Andee
2 years ago

Very useful but any suggestions on how to get the Mulesoft server timezone in Mule4? I don’t want to hardcode in case it changes.

Mina
Mina
1 year ago

Hi I have date as in this format- 24-AUG-22 01.33.04.306011000 PM
How can i convert it into yyyyMMDD format?

R K
R K
8 months ago

How do I get Noda(IANA) timezone from date?

Example: I need ” Australia/Sydney” from “2023-07-31T16:12:08.945069+11:00”

bolla
bolla
4 months ago

i want calculate days from today to lastmonth second monday