DataWeave Recipe: Adding days to a date with the input provided as an integer

Share this:

The challenge

I just had a requirement to calculate a new date by adding days to a another date. The provided input is an integer that represents the number of days after 01 January 1900. The required output is essentially that date in dd-MM-yyyy format. Essentially, it’s to calculate this.

Days from 1900-01-01, 00:00:00 UTC time

But this code recipe would work if you need to do the same with another other start date.


DateWeave provides date time operators (link). However, it requires a period of time object to be added. Therefore, you need to convert the integer representing the days to a period. Then you add the period with the start date.

The code

Assuming the input is saved in a flow variable called dayAdd and the start date is 01 Jan 1900, the following would be the code to use to calculate the output date.

%dw 1.0
%output application/json
  added: (|1900-01-01| + ("P" ++ flowVars.dayAdd as :string ++ "D") as :period)

With the sample data of dayAdd = 42955, the following is the output of the DateWeave code.

  "calculatedDate": "2017-08-10"

Hope this helps!


Adding days (integer) to a date in DataWeave

Check out more Mule tips in my Mule Cookbook.

What’s DataWeave?

The DataWeave Language is a simple, powerful tool used to query and transform data inside of Mule. It can be implemented to:

  • graphically map fields by dragging one attribute to another, just like you were able to with the now deprecated DataMapper, or
  • leverage its powerful object-oriented language that’s specially designed to make writing transformations quick, without compromising maintainability.

More information here:

What’s Mule?

Mule is the lightweight integration runtime engine that allows you to connect anything, anywhere. Rather than creating multiple point-to-point integrations between systems, services, APIs, and devices, you use Mule to create applications that intelligently manage message routing, data mapping, orchestration, reliability, security, and scalability between nodes. Plug other systems and applications into Mule and let it handle communication between systems, allowing you to track and monitor your application ecosystem and external resources.

Share this:

You may also like...

1 Response

  1. Grady Congdon says:

    Many thanks! I was adding the “|” pipes and it was failing for me

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.