Converting String to DateTime Format with Leading Zeros in UiPath

I need help with converting a string 125202471300 into a DateTime format in UiPath. The desired output format is MM/dd/yyyy HH:mm:ss.

The string is in the format MMddyyyyHHmmss, where:

  • MM is the month (2 digits),
  • dd is the day (2 digits),
  • yyyy is the year (4 digits),
  • HH is the hour (2 digits),
  • mm is the minute (2 digits),
  • ss is the second (2 digits).

However, when I try to parse the string, the date and time are not showing the leading zeros correctly (e.g., for the day “05” instead of “5”). Can you help me ensure the correct formatting with leading zeros?

I would appreciate any help you can offer!

Thank you in advance.

@Murthy_Chethana,

Use this snippet in assign activity to get date in desired format.

strOutputDate = DateTime.ParseExact(dateString, "MMddyyyyhhmmss", System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy hh:mm:ss")

@Murthy_Chethana

Please try this… I beleive HH is 24 hour format

strOutputDate = DateTime.ParseExact(dateString, "MMddyyyyHHmmss", System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy HH:mm:ss")

Cheers

Hello @Murthy_Chethana your given input 125202471300 is not following the format MMddyyyHHmmss format.

Can you tell us, what is going the format of input String. Few example of input string with format would be great to understand the requirement.

The format that you have is MdyyyyHmmss or MdyyyyHmss, have you tried these 2?

I am getting error as Assign: String ‘’ was not recognized as a valid DateTime.

While Extracting date time such as 12112024123044 the format u r suggesting wont work

12112024123044 this can be another format

give us few more examples like this with different date and time

Hello @Murthy_Chethana

Please try this:

Assign str_Date = "125202471300"
Assign arr_Formats = {"MMdyyyyHmmss","MMddyyyyHHmmss", ... add more formats as desired...}

Assign str_ConvertedDate = DateTime.ParseExact(str_Date,arr_Formats, System.Globalization.CultureInfo.InvariantCulture,DateTimeStyles.None).ToString("MM/dd/yyyy HH:mm:ss”)

Just add more formats that the string could have.

Regards
Soren

The following errors were encountered while processing the workflow tree:
‘VisualBasicValue’: Compiler error(s) encountered processing expression “DateTime.Parse(strExtractedDate, “MMddyyyyHHmmss”, System.Globalization.CultureInfo.InvariantCulture)”.(2) : error BC30512: Option Strict On disallows implicit conversions from ‘String’ to ‘IFormatProvider’.
(2) : error BC30311: Value of type ‘CultureInfo’ cannot be converted to ‘DateTimeStyles’.
I am seeing this issue

Please add import System.Globalization for it to recognize DateTimeStyles.None

Regards
Soren

format recognition is working from left to right and will fail on ambigious formatted date strings

in such case we can stabilize the flow with Regex and format detection and will handle (e.g. sorting out / flowing back for resolution) for the not recognized formats

issue with this logic would be, if date comes as 1112024232411 then it is not clear if date is 1/11/2024 232411 or 11/1/2024 232411

1 Like