Power Platform

Convert strings to date format for SharePoint in MS Flow

Ok, so my SharePoint column is expecting a date/time per item in a loop, but all I have is a set of strings. Challenge accepted.
I’m documenting this here for my own future reference, but this is also to show you how to get strings to date format in microsoft flow using expressions.

I’ve done it this way, because my PowerApp allows a user to enter in a date from a date selector, then enter in the time using hours and minutes. It’s very user friendly, so I really wanted to turn these strings into a date format once they were submitted in the PowerApp.

My powerapp also submits multiple items, so these dates will only be available in a loop (apply to each) in Flow.

My date/time variables in Flow are named as follows:

  • lineTripArrivalDate
  • lineTripArrivalHour
  • lineTripArrivalMinute
  • As above for ‘Departure’

You can use the formatDateTime() function in the Microsoft Flow expressions in order to turn a string into a date/time format that will be a valid input for the SharePoint column.

You can also use the concat() function to pull different Flow variables together in the one string.

So my final Flow expression for turning a string into a date/time in an ‘apply for each’ loop as follows:

formatDateTime(
concat(
items('Apply_to_each')?['lineTripArrivalDate'],
' ',
items('Apply_to_each')?['lineTripArrivalHour'],
':',
items('Apply_to_each')?['lineTripArrivalMinute'],':00'
),
'yyyy-MM-ddTHH:mm:ss'
)

Converting at the top level

Ok, so you’re probably not doing this in a loop yet. I just went straight for the hard stuff. Here’s how I would do this in the top level.

If you use variables in your flows (like I do), then your date/time variables at the top level would look something like this:

  • variables(‘tripDepartureDate’)
  • variables(‘tripDepartureHour’)
  • variables(‘tripDepartureMinute’)

For these variables, my formatDateTime expression would look like this:

formatDateTime(
concat(
variables('tripDepartureDate'),
' ',
variables('tripDepartureHour'),
':',
variables('tripDepartureMinute'),
':00'
),
'yyyy-MM-ddTHH:mm:ss'
)

Let’s say you’ve performed actions in a section called ‘SectionName’, then your variables would be as follows:

  • body(‘SectionName’)?[‘{tripDepartureDate}’]
  • body(‘SectionName’)?[‘{tripDepartureHour}’]
  • body(‘SectionName’)?[‘{tripDepartureMinute}’]

Then the formatDateTime code for the above variables would look like the following:

formatDateTime(
	concat(
		body('SectionName')?['{tripDepartureDate}'],
		' ',
		body('SectionName')?['{tripDepartureHour}'],
		':',
		body('SectionName')?['{tripDepartureMinute}'],
		':00'
	),
	'yyyy-MM-ddTHH:mm:ss'
)

Happy Flow-ing with strings to dates!

Leave a Reply

Your email address will not be published. Required fields are marked *