Setting date values in dateTime attribute

1
I have two values To and From which are of DateAndTime. These are being used in a page where a drop down menu is of type enumeration called Year and Month (which are also in the same entity as attributes) and is sending the values in a microflow and then the enum values are parsed into DateAndTime format in a new Variable. I then want to put those values in the To and From attribute where From will be (YearValFromEnum-MonthValFromEnum-”BeginningOfTheMonth”) and To attribute will be (YearValFromEnum-MonthValFromEnum-”EndOfThatMonth”). The Enum in year is 2018 and 2019, The enum values in Month are numbers since I want to parse them in the parseVariable. They were ‘January’, ‘February’ etc. before. How can this be done? eg. if the user selects 2018 november then From should have (2018, 11, 01) and To should have (2018, 11, 30). I want the last day of the month to be calculated since different months have different last date and a leap year will have different last date for February. In the future if a year is added to enumeration this shouldn’t be a problem. The picture below shows the microflow im making. Also the parse funtion for month and year is this: parseDateTime(toString($YearVarFromTimesheetsPage),'yyyy')
asked
5 answers
0

Lets say your Enums are something like that:

notice the underscore in the Name. It is there as MX doesn’t allow Numbers as Names.

You could create a variable DateTimeStr

replaceAll(toString($ExcelReport/Year), '_', '') +
replaceAll(toString($ExcelReport/Month), '_', '-') + '-01'

The above will give you a parsable string to create the From DateAndTime. What it does is:

  1. getting the Year Enum and transform it to a string
  2. replaces the underscore with nothing (removes it) so returning just the year as a string e.g. ‘2018’
  3. then the same for the month only instead of removing the underscore it replaces it with a dash giving e.g. ‘-02’
  4. and in the end concatenates the year, month and ‘-01’ (day)  to something like ‘2018-02-01’

Then parse the DateTimeString to create the From DateAndTime variable like this: 

parseDateTimeUTC($DateTimeStr, 'yyyy-MM-dd')

and to get the To DateAndTime create a variable as follows: 

addDaysUTC(addMonthsUTC($DateTimeFrom, 1), -1)

then change the From and To attributes in your Object and you’re good to go.

 

answered
1

Not sure how good is Javain your environment.

One approach is to crate a java_action passing the dates as parameters.  Use the java 8   (java.time) API to manipulates the dates, and return the desired results.   The Java action can be expended further, to use a custom class, with static methods.

Have designed a date time utility using the time API.  However, was for different purpose, such as calculating a future date and time for a running task, running at different intervals.

answered
0

Yes I applied this method but it gives me an error:

I tried with replaceall but it still doesn’t work.

answered
0

Its giving me this error when I parse it into integer.

com.mendix.core.CoreException: com.mendix.modules.microflowengine.MicroflowException: Failed to evaluate expression, error occurred on line 1, character 1
parseInteger($ConvertYearIntoString)
^


    at Common.SUB_SetToAndFrom (CreateOrChangeVariable : 'Create Integer/Long variable')
    at Common.ACT_Process_Enhanced (SubMicroflow : 'SUB_SetToAndFrom')

Advanced stacktrace:
    at com.mendix.basis.component.InternalCoreBase.execute(InternalCoreBase.java:389)

Caused by: com.mendix.modules.microflowengine.MicroflowException: Failed to evaluate expression, error occurred on line 1, character 1
parseInteger($ConvertYearIntoString)
^


    at Common.SUB_SetToAndFrom (CreateOrChangeVariable : 'Create Integer/Long variable')
    at Common.ACT_Process_Enhanced (SubMicroflow : 'SUB_SetToAndFrom')

Advanced stacktrace:
    at com.mendix.languages.mxexpressions.MxExpression.evaluate(MxExpression.scala:21)

Caused by: com.mendix.languages.expressions.ExpressionException: java.lang.NumberFormatException: For input string: "01-Jan-2019 12:00:00 AM"
    at com.mendix.languages.expressions.Expr.evaluate(Expr.java:34)

answered
0

I got the values. Thank you so much for the help :)

answered