Setting date to End Of Month

0
I have a requirement that states that if a user enters the date as either the 28, 29, 30, or 31 of a month, then that date defaults to the end of that month, and all other months are set to the end of month as well. For example, if I enter the date as 1/29, it will be automatically changed to 1/31, and if a date in February is to be displayed, it will be 2/28 (or 2/29, depending on the year), and March would be 3/31, etc. Is there a tool that does such formatting? Or is it something I will have to create in a microflow?
asked
4 answers
4

Hi Adam,

You can easily execute this within a microflow using the following functions on an input dateTime object called "$inputDate":

addDays( addMonths( trimToMonths($inputDate), 1), -1)

Essentially, you trim your input date to the beginning of that month. Then you add a month to the modified DateTime object. Finally, you subtract one day from the result to get the last day of the previous month.

That should solve your issue immediately. Maybe adding a little logic to check the input date DAY would solve it entirely.

Happy modelling!

answered
1

Adam,

I don't think there is a standard function to do this but with a little java this is easy. The code below takes a datetime as input, and returns the datetime as the last day of the month taken from the input.

Hope this helps.

 import java.util.Calendar;
import java.util.Date;
import com.mendix.systemwideinterfaces.core.UserAction;

/**
 * 
 */
public class GetLastDay extends UserAction<java.util.Date>
{
    private java.util.Date inputDate;

    public GetLastDay(java.util.Date inputDate)
    {
        super();
        this.inputDate = inputDate;
    }

    @Override
    public java.util.Date executeAction() throws Exception
    {
        // BEGIN USER CODE
        Calendar cal = Calendar.getInstance();
        cal.setTime(inputDate);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date resultdate = cal.getTime();
        return resultdate;
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public String toString()
    {
        return "GetLastDay";
    }

    // BEGIN EXTRA CODE
    // END EXTRA CODE
}
answered
1

Better use addSeconds in stead of addDays -1. In this case addDays returns a 0:00 time and puts you at the beginning of the last day of the months and can result in unexpected result when retrieveing data using a date filter AddSecond returns 23:59 time and really puts you at the end of the last day of the month

answered
0

Adam You can use the XPATH system variable [%EndOfCurrentMonth%] in a microflow. More information can be found here: XPath Keywords in Reference Guide 4.0

Mike

answered