ProstDev ProstDev
Tutorials Jun 15, 2021 · 2 min read

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

I needed to get a person's age. I started using "daysBetween" but this was a wrong way. In this post, I'll show you how to use the "between" function from the Periods module to retrieve the number of years, months, or days between two dates.

By Alex Martinez
Thumbnail: How to retrieve the number of years, months, or days between two dates in DataWeave 2.0

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

FAQs

Frequently asked questions about this post.

  • How do I get the number of years, months, or days between two dates in DataWeave 2.0?

    Use the between function from the Periods module, which returns a Period like "P31Y2M19D", and then pull out each part with a field selector: period.years gives the years, period.months the months, and period.days the days.

  • How do I import the `between` function?

    Because between lives in a different module, you import it first. In this post it is brought in explicitly with import between from dw::core::Periods.

  • Why not use `daysBetween` to calculate someone's age?

    The author started with daysBetween and then manually counted the days to convert them to years, but that led to a wrong answer, which is why the post switches to the between function from the Periods module instead.

  • How can I get the same outputs as the post instead of today's date?

    The post uses a specific date, "2021-03-20" as Date, rather than the now function so you receive the same outputs as the author; you can use now() instead of the fixed date to refer to today's date.

Search

Loading search…