Comparing dates in an XPath query

5
We wish to restrict an XPath query based on a date. Initially we wrote: [effectiveDate = $anotherObject/effectiveDate] The problem with this is that effectiveDate is a Mendix Date datatype which includes the time to the second, and matches the whole field. We want to select objects whose effectiveDate is the same day, but ignoring the hours, minutes and seconds. Seemingly, many of the date functions available in microflows aren't available in XPath. How can we perform the comparison to obtain a list of objects whose effectiveDate falls on the same day as that of anotherObject?
asked
4 answers
10

You're right that those functions are not available in Xpath, but as Michel suggested, you can use the microflow function trimToDays. There are two different strategies you can follow, depending on how you use the date attributes:

  • If you use those attributes purely to store a date (meaning the time part is never relevant), you can "remove" the time part by using trimToDays when setting the attribute. This way you can use the (simple) Xpath restriction you mentioned in your question. The disadvantage however is that you must be careful never to commit a "non-trimmed" date, or this solution won't work (this also means you can't use the token [%CurrentDateTime%], but instead must use [%BeginOfCurrentDay%]).
  • If you don't want to trim the time part of the attributes in your database, you can use an Xpath restriction like this:

    [effectiveDate >= $startOfDay and effectiveDate < $endOfDay]
    

    Using this solution, you first need to create the $startOfDay and $endOfDay microflow variables:

    $startOfDay = trimToDays($anotherObject/effectiveDate)
    $endOfDay = addDays($startOfDay, 1)
    
answered
7

You might also consider retrieving:

[day-from-dateTime(effectiveDate) = day-from-dateTime ($anotherObject/effectiveDate)]
[month-from-dateTime(effectiveDate) = month-from-dateTime ($anotherObject/effectiveDate)]
[year-from-dateTime(effectiveDate) = year-from-dateTime ($anotherObject/effectiveDate)]

 

This results in a ‘cleaner’ microflow in my opinion.

 

 

answered
2

Martin,

I guess the easiest way is to use microflow the set the second effictiveDate and use the function 'trimtoDays'

answered
0

How we can do these date comparisons in Mendix Native XAPTH?

 Any ideas

Thanks in advance

answered