Reputation: 91
I am able to get the days passed in current month. But don't know how to get days left in current month.
For example in this month (June) days passed=7 and days left = 23
%dw 2.0
output application/json
---
{
"daysPassed" : now().day -1,
"daysLeft" : ""
}
Upvotes: 0
Views: 1084
Reputation: 25699
DataWeave doesn't seem to have a built-in function to compute the end of month, so I calculate it using some date arithmetic.
%dw 2.0
output application/json
fun daysLeftOnMonth(d) = do {
var nextMonthDate = d as Date + |P1M|
var endOfMonth= ( nextMonthDate - ( ("P$(nextMonthDate.day as String)D" as Period) )).day
---
endOfMonth - d.day + 1
}
---
{
"daysPassed" : now().day - 1,
"daysLeft" : daysLeftOnMonth(now())
}
To explain more in detail, first I add a month to the current date:
var nextMonthDate = d as Date + |P1M|
Then I go to the the beginning of the month, less one day, so I get the last day of the current month:
nextMonthDate - (("P$(nextMonthDate.day as String)D" as Period)
The trick was how to rest a dynamic time period in DataWeave. I had to search for it. You have to construct the period as a string, then coerce it into a period.
Then I just substract the days passed to the number of day of the last day of the month.
Upvotes: 3
Reputation: 4303
Something similar to this and you can always build the leap year check into this.
%dw 2.0
output application/json
var thirtyDayMonths = [4,6,8,10]
---
{
"daysPassed" : now().day -1,
"daysLeft" : if (thirtyDayMonths contains now().month)
(30-now().day)
else if (now().month == 2)
(28-now().month)
else (31-now().month)
}
Upvotes: 2