how to convert string to date and time

0
hello  how i can convert string to date and time  input 2023/09/19 0:00 →Date and time output 202309 →Date and Time   I use "parseDateTime", but it is error.
asked
5 answers
1

Hi Fuji,

 

I can help you solve this problem. 

try this method : 

 

image.png

 

Feel free to ask questions if any?

 

answered
1

Hi, Fujii Hinako

You can do that using date time functions in a microflow expression. Let say you have a string variable containing date string as '801022'. Now when creating the object of you entity where you have to set some DateTime attribute, use this expression to get the formatted date.  

parseDateTime($DateString, 'yyMMdd')

The ‘yyMMdd’ specifies the format in which date is represented in the string. As the string ‘801022’ represent year (yy) first, then month (MM) and then day (dd) so format string becomes yyMMddFor time zone adjustment you can use any of these functions: parseDateTime OR parseDateTimeUTCPlease refer to documentation for details: https://docs.mendix.com/refguide/parse-and-format-date-function-callsAfter the above logic, the date will be stored in Mendix object correctly. Now when you display that date on a page in your application, you can choose how it will be displayed (date format) 

 

I hope it will help you!

 

Thank You

K.Ravi Kumar

answered
1

Hi Fuji,

The 

parseDateTime($DateString, 'yyMMdd')

It will only work if the DateString string that is coming as an input is exactly in the same format as what you are defining in your parse function.

otherwise, it will fail everytime,

If you are expecting strings of various formats, then you need to build the logic in such a way, that if the first parse fails, then it should execute second parse function with other string.

Let me know, if you have any issues,

Hope it helps!

answered
1

In addition to the other answers, if you want the output to be "202309" you need to use the formatDateTime function. I assume this is 4 digits for the year and two digits for the month. You pass in a datetime variable and it returns a string.

 

formatDateTime($DateTimeVariable, 'yyyydd')

 

https://docs.mendix.com/refguide/parse-and-format-date-function-calls/#3-formatdatetimeutc

 

I hope this helps.

answered
1

Now combine the two functions....

 

formatDateTime (parseDateTime($InputValue, 'yyyy/MM/dd'), 'yyyyMM')

answered