Do you need to convert a string value to a date in your flow?
Sometimes you might come across data that looks like a date but is actually a text string in Power Automate.
If you want to perform any manipulation such as formatting the date or adding days to the date, then you will first need to convert the text to a date that Power Automate will recognize.
This post is going to show you ways to turn a string to a date in Power Automate.
Valid Datetime Strings
In this example, there is a date inside a Compose action named Date String.
The date 31/12/2022 is formatted as dd/MM/yyyy. This is a very common date format used by a lot of people around the world.
Unfortunately, if you try to use this as a date in Power Automate you will get an error telling you the provided date time string is not valid.
The DateTime string must match ISO 8601 format which is yyyy-MM-dd.
This means you will need to parse your date and rearrange it into the correct format.
Parse the Date into Components
Since the example date has both a date and time component, the first step will be to get only the date part of the string.
This can be done with the split function. This converts the string into an array based on a delimiter.
split(outputs('Date_String'),' ')[0]
You can use the split function to split the date and time based on the space character into an array with two items and then get the first item using the index [0]
.
The above expression will get just the date part of the datetime string. In this example, it will return the string 31/12/2022.
split(split(outputs('Date_String'),' ')[0],'/')
Now you can use the split function again based on the forward slash /
character to get an array with the day, month, and year.
The above expression will take the full date time string and return this array ["31","12","2022"]
in the Compose action named Parse.
Join Day, Month, and Year Components into a Date
Now that you have each of the day, month, and year components in an array you will be able to join them in the required yyyy-MM-dd format.
you can joining these all together using the concat function and using the index number to get the values of the array.
concat(outputs('Parse')[2],'-',outputs('Parse')[1],'-',outputs('Parse')[0])
The above formula will combine the year, month, and day from the array created in the Parse compose action.
- The year is the third item in the array so this has an index of 2 and
[2]
will access this value. - The month is the second item in the array so this has an index of 1 and
[1]
will access this value. - The year is the third item in the array so this has an index of 0 and
[0]
will access this value.
These are all combined with a dash character -
between them to create the date in the proper format.
In this example, the year, month, and day componentent in the array are in reverse order to which you need to join them.
This means, you can use the reverse function to put them in the order needed.
Then you can use the join function to concatenate the components with a dash. This is a more elegant approach to creating the date in the proper format.
join(reverse(outputs('Parse')),'-')
The above expression formula will reverse the order of the array and then join the results with the dash character.
This formula works because you’re easily able to get the array items in the needed order using the reverse function.
Either way you combine the values, the result is the correct ISO date format needed in Power Automate!
Conclusions
Dates in Power Automate require a specific yyy-MM-dd format to work in any of the date time functions.
This means you’re going to need to convert any dates into the format through the use of text parsing function.
Have you ever come across the invalid date error? How did you convert your text string to a date? Let me know in the comments!
Thanks John, your article is exactly what i needed to convert a date from a format to another. Keep up your good work.
No problem, glad it helped!
Hi John, thanks for this.
How would you convert a date which has the format of the basic underlying number e.g. “45230”
Try this expression
@{addDays('1899-12-30',int(outputs('Compose')),'yyyy-MM-dd')}