1

Hi, I need some help with a calculation in Mendix. I need to find numberofDays within a date range, but ignore if there is a Leap Day (29th Feb). Any suggestions on how to achieve this in Menidx? Thank you,

asked

Ravi Kiran Chaliki

4 answers

3

Hi,

I am interested in the use case where the day is willingly removed. But well – there are multiple ways in doing so.

You will get the days between like this:

`daysBetween(dateTime(2007, 2, 13, 1, 1, 1), dateTime(2007,1,1,1,1,1))`

then you have to check whether or not the current year is a leap year. I just tested a sneaky way of doing that as follows:

```
parseDateTime('2020-02-29', 'yyyy-MM-dd') // If your DateTime variable is in fact created as the 29th of february, then it is a leap year (like this year)
parseDateTime('2021-02-29', 'yyyy-MM-dd') // if it is not a leap year, Mendix will generate the 1st of March in this case!
```

Edit: As Dave said, there is another way of checking for leap year. I found that model share.

https://modelshare.mendix.com/models/6fc9b551-ac2f-4070-a254-2c2afe15217d/check-leap-year

As model share will be discontinued, here a description:

input of a datetime ‘testDate’, output boolean Expression

- → Create Integer [ parseInteger(formatDateTime($testDate, 'yyyy')) ] → o [ if(($year mod 400 = 0) or (($year mod 4 = 0) and ($year mod 100 != 0))) then true else false ]

answered

Lukas Tost

0

Thanks Lukas and Dave.

I have used the same approach to determine the leap year. **However, still having problem finding if the leap day falls between the range selected.** Sorry guys, I am still finding my way around Mendix functions.

@Lukas – Sample use case in Insurance terms the days covered will always include the Leap day, though the premium will be collected only for 365 days :) Hope this explains your question.

answered

Ravi Kiran Chaliki

0

This website ( http://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm )is quite helpfull for the mathematics behind it. A pity you can not upload to model share anymore because then you could have my microflow for calculating Long year.

Regards,

Ronald

[EDIT]

Create a microflow with parameter integer and parameter name year. Then create a Decimal PY variable and put this inside it: $year + floor($year div 4) - floor($year div 100) + floor($year div 400). Create another integer variable yearMinus1 and put this inside: $year - 1. Create another variable Decimal PYMinus1 and put inside this: $yearMinus1 + floor($yearMinus1 div 4) - floor($yearMinus1 div 100) + floor($yearMinus1 div 400). Then create a split and put this inside:

$PY mod 7 = 4 or

$PYMinus1 mod 7 = 3

If true then it is a Long year and false when it is not.

answered

Ronald Catersels

0

If you want to drop to Java, IsLeap is one line in a Java Action.

`return java.time.Year.isLeap(this.Year);`

answered

Robert Price