top of page

How to retrieve the number of years, months, or days between two dates in DataWeave 2.0

The video version of this article can be found at the end of the post.



I was working on a pretty fun API for some demo purposes, and I needed to get a person’s age. Of course, my first instinct was to jump right into it and start using different core functions. (By the way, if you haven’t seen my DataWeave 2.0 core functions cheat sheet, check it out here!)


I started using “daysBetween”, and then I manually counted the days to convert them to years. You know, like, 365 days equals a year, so divide all the days in between...You know what? It’s not even worth explaining how I got to a wrong answer.


In this post, I’ll show you how to use the “between” function from the Periods module (which I can’t seem to find in the Mule docs, but here’s a link to what a Period is in DataWeave) to retrieve the number of years, months, or days between two dates.




Using the “between” function


Since the “between” function is from a different module, we’re going to need to import that module first. I’m going to explicitly import just this function from the Periods module.


%dw 2.0
output application/json
import between from dw::core::Periods
---
payload

Note: For a detailed explanation of the different ways to import a module, please refer to this post.


I’ll create a variable that keeps the person’s birth date while not having to copy and paste for different tests.


%dw 2.0
output application/json
import between from dw::core::Periods
var birthdate = "1990-01-01" as Date
---
payload

Now let’s create a variable that keeps the period that we’ll get back from the function (so we can refer to it in different fields).


%dw 2.0
output application/json
import between from dw::core::Periods
var birthdate = "1990-01-01" as Date
var period = between("2021-03-20" as Date, birthdate)
---
payload

Note that I’m using a specific date instead of the function “now” because this way, you’ll receive the same outputs as me. You can use “now()” (without the quotes) instead of “2021-03-20” to refer to today’s date.


Finally, let’s create an object with different fields to see the other numbers it returns for years, months, and days.


%dw 2.0
output application/json
import between from dw::core::Periods
var birthdate = "1990-01-01" as Date
var period = between("2021-03-20" as Date, birthdate)
---
{
   period: period, // "P31Y2M19D"
   years: period.years, // 31
   months: period.months, // 2
   days: period.days // 19
}

In the first field, you’ll see the complete period that was returned, and we can extract the specified number of years, months, or days from this period just by using a field selector.



That’s it! Let me know if this was useful to you. Hopefully, I saved you some minutes or hours of manual work.


Prost!

-Alex



Getting to the point (under 30 seconds) video:



3,099 views2 comments
bottom of page