How to compare two dates with only month and year in mule

55 Views Asked by At

My scenario is to check if the Month and year (in date value) is less than the current month and year (in current date value).

The condition I need to implement is this

if source-month&year  < current-month&year Yes else No 

Scenario 1:current date = 03/29/2024 (MM/dd/yyyy) and source incoming date = 03/01/2021 (MM/dd/yyyy). In this case it should give me Yes

Scenario 2: current date = 03/29/2024 (MM/dd/yyyy) and source incoming date = 03/01/2024 (MM/dd/yyyy). In this case it should give me No.

I tried using .month and .year but it always giving result as No

3

There are 3 best solutions below

0
Tony Stark On

Try this and let me know if it is working as you expected .upvote if it is working

%dw 2.0
output application/json

var currentDate = now()
var currentYear = currentDate.year
var currentMonth = currentDate.month
var inputDate = "03/01/2024" as String {format: "MM/dd/yyyy"}
var inputYear = (inputDate as Date {format: "MM/dd/yyyy"}).year
var inputMonth = (inputDate as Date {format: "MM/dd/yyyy"}).month

---
if (inputYear < currentYear or (inputYear == currentYear and inputMonth < currentMonth)) 
    "Yes"
else 
    "No"
0
Ryan Hoegg On

I'd probably turn both of the dates into the first of the month and then compare.

%dw 2.0
output application/json

var a = |2024-03-23T02:47:40.014782Z|
var b = |2024-03-30T02:48:34.216274Z|

fun firstOfMonth(d) = 
    d as String {format: "yyyy-MM-01"} as Date
---
{
    a: a,
    b: b,
    less: firstOfMonth(a) < firstOfMonth(b)
}
0
ASHISH SINGH On

You can try with the below snippets. It will work as per your requirement.

%dw 2.0
output application/json
var inpDate = "03/01/2024" as Date {format:"MM/dd/yyyy"}
var today = now() as Date {format:"MM/dd/yyyy"}
---

{
    "inpDate": inpDate,
     today:today,
    "isCheck":  (inpDate.month + inpDate.year) < 
(today.month + today.year)
}