Covert String to Date time

Hi,

I want to convert this string “04/0‎4/‎2022 ‏‎2:35 PM” to dateTime variable.
dateString = “04/0‎4/‎2022 ‏‎2:35 PM”
arr_strFormats = {“M/d/yyyy h:mm tt”,“MM.dd.yyyy h:mm tt”}

I tried to do this but it dint work: DateTime.ParseExact(dateString,arr_strFormats,System.Globalization.CultureInfo.InvariantCulture,DateTimeStyles.None)

Error: Assign: String was not recognized as a valid DateTime.

1 Like

Hey @Sairam_RPA

Your formats should be updated,

MM/dd/yyyy h:mm tt

Add the above format in your array formats.

Hope this helps

Thanks
#nK

No that dint work. still see the same error

1 Like

Hey @Sairam_RPA

Working fine, Kindly have a look…

image

image

DateTime.ParseExact(str_Date, "MM/dd/yyyy h:mm tt", System.Globalization.CultureInfo.InvariantCulture, Nothing)

Thanks
#nK

Interesting I get a error message when I use this.

Log Message: String was not recognized as a valid DateTime.

image

Log Message: String was not recognized as a valid DateTime.

Hi @Sairam_RPA ,

It seems that there is something wrong with the Input Data. It is not in the right format. It appears as Simple String, but it is not. It is as you have said very Strange.

Could you type the Input data in your expression and Check. Do not Copy Paste .

Are you using a Copy to Clipboard activity and storing it in the dateString variable ?

For Checking, use Regex website :
image

1 Like

Hey @Sairam_RPA

Yes as @supermanPunch suggested, if you would have copy pasted the string from some source, there is a chance of invalid format which may not be visible.

Thanks
#nK

This is the value I get for last saved dateTime of the excel file: ‎4/‎4/‎2022 ‏‎2:35 PM. ( This is what I have to use)

When I coped and pasted there it changed to 04/04/2022.

image

1 Like

@Sairam_RPA Could you Check the value by using Write Line Activity ?

1 Like

Hey @Sairam_RPA

Most probably looks like you are getting spaces in between.

Thanks
#nK

image
I used write line & Log Message this is what I get.

{
“message”: “‎4/‎4/‎2022 ‏‎2:35 PM”,
“level”: “Trace”,
“logType”: “User”,
“timeStamp”: “11:23:43”,
“processVersion”: “1.0.0”,
“jobId”: “29dbb23d-646b-4308-ac1c-48f2ccc3830c”,
“robotName”: “*”,
“machineId”: 0
}

{
“message”: “‎4/‎4/‎2022 ‏‎2:35 PM”,
“level”: “Warning”,
“logType”: “User”,
“timeStamp”: “11:23:43”,
“processVersion”: “1.0.0”,
“jobId”: “29dbb23d-646b-4308-ac1c-48f2ccc3830c”,
“robotName”: “*”,
“machineId”: 0,
“fileName”: “test”
}

1 Like

Hey @Sairam_RPA

But still it is failing for this ?

Thanks
#nK

Yes It is still throwing an error. Log Message: String was not recognized as a valid DateTime.

1 Like

@Sairam_RPA , Could you try the Below Approach :

  1. Assuming that there are hidden Characters in the String. We will first remove them :
dateString = System.Text.RegularExpressions.Regex.Replace(dateString, "[^\x20-\x7F]", "")
  1. Next, Check with your original Expression :
DateTime.ParseExact(dateString,arr_strFormats,System.Globalization.CultureInfo.InvariantCulture,DateTimeStyles.None)

Let us know if it doesn’t work

2 Likes

Getting Last Saved Time of a file and adding the data to a Data table. ( FileName| lastsavedDate)

Here is the XAML
Test1.xaml (18.8 KB)

Hope this helps ppl needing the last saved date and convering it to Date time format and saving in DT

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.