Dataweave:当月过去的天数和剩余的天数
Dataweave : Days passed and days left in current month
我可以得到当月过去的天数。但是不知道如何得到当月的剩余天数。
例如,本月(6 月)过去的天数 = 7,剩下的天数 = 23
%dw 2.0
output application/json
---
{
"daysPassed" : now().day -1,
"daysLeft" : ""
}
与此类似,您始终可以将闰年检查纳入其中。
%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)
}
DataWeave 似乎没有内置函数来计算月末,所以我使用一些日期算法来计算它。
%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())
}
为了更详细地解释,首先我在当前日期上加一个月:
var nextMonthDate = d as Date + |P1M|
然后我去月初,少一天,所以我得到当月的最后一天:
nextMonthDate - (("P$(nextMonthDate.day as String)D" as Period)
诀窍是如何在 DataWeave 中休息动态时间段。我不得不寻找它。您必须将句点构造为字符串,然后将其强制转换为句点。
然后我将传递的天数减去该月最后一天的天数。
我可以得到当月过去的天数。但是不知道如何得到当月的剩余天数。
例如,本月(6 月)过去的天数 = 7,剩下的天数 = 23
%dw 2.0
output application/json
---
{
"daysPassed" : now().day -1,
"daysLeft" : ""
}
与此类似,您始终可以将闰年检查纳入其中。
%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)
}
DataWeave 似乎没有内置函数来计算月末,所以我使用一些日期算法来计算它。
%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())
}
为了更详细地解释,首先我在当前日期上加一个月:
var nextMonthDate = d as Date + |P1M|
然后我去月初,少一天,所以我得到当月的最后一天:
nextMonthDate - (("P$(nextMonthDate.day as String)D" as Period)
诀窍是如何在 DataWeave 中休息动态时间段。我不得不寻找它。您必须将句点构造为字符串,然后将其强制转换为句点。
然后我将传递的天数减去该月最后一天的天数。