How to convert a string("12/12/2019 1:33 PM") into a DateTime variable

I am not able to convert a 12 hr format string to 24 hr DateTime.


I tried this but getting error like this.

Can you try this @Alamgir03

DateTime.ParseExact(Your Time Here,"HHmm", Nothing).ToString("hh:mm tt")
1 Like

I have to keep the date also. I tried this.
Datetime.parseexact(str_string1,“MM/dd/yyyy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture).tostring(“MM/dd/yyyy HH:mm:ss”)

But getting error like:

String was recognized as valid datetime.

1 Like

Hi
We need to check the date format of str_string1
Use a writeline activity before to this expression and mention the input as str_string1 and lets c what is the date format it has
Based on that we can conclude the expression
Cheers @Alamgir03

in your parse exact, try “G” as the format…

datetimeParse_24.xaml (5.5 KB)

I have tried your input string getting the expected output.

Please have a look.

Thanks!

2 Likes

Thanks @kadiravan_kalidoss,

Its working fine giving input in the same format as string.
But In my code, I am scraping it from Ui and used same code to parse but it is giving error now.


@Palaniyappan

Thanks for your reply.
Below is the code with write l

ine and the error.

My extracted date is Coming(“12/13/2019 11:36 AM”) with actual length 19 but while using mystringvariable.Length, I am getting it as 26.

I removed the \r\n also but still I am getting length as 24.

How do I get the actual length as 19(“12/13/2019 11:36 AM”).

Please suggest.

Thanks,
Alamgir

1 Like

Hello,

try with datetime.parse instead of datetime.parseexact and check.

DateTime.Parse(“12/13/2019 1:33 PM”).ToString(“MM/dd/yyyy HH:mm:ss”)

Thanks,

Hi
Kindly try with this expression

str_date = IF(System.Text.RegularExpressions.Regex.IsMatch(str_string1.ToString,”[0-9]+:[0-9].”), Datetime.parseexact(str_string1.ToString.SubString(0,19).Trim,“MM/dd/yyyy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture).tostring(“MM/dd/yyyy HH:mm:ss”),
Datetime.parseexact(str_string1.ToString.SubString(0,19).Trim,“MM/dd/yyyy h:mm tt”,System.Globalization.CultureInfo.InvariantCulture).tostring(“MM/dd/yyyy HH:mm:ss”))

Cheers @Alamgir03

Why IF here ??
I am still getting the same issue.

Sometimes the hour can be like 12:33 or 1:33
So the length and format differs right
@Alamgir03

Yes,
But, what is the issue in the length of this(“12/13/2019 1:33 PM”) string. It is giving length as 25.
How to remove the special character?? I think some hidden characters are there.

Like how we will be getting the input
@Alamgir03

After scraping by using get full text activity and storing that in a string variable.
Then, using that variable in Parse Exact.

Before that i am printing that variable and getting the same output.There are some extra hidden characters(\r]\n) . I removed that also but getting the same error.

Hello,

Its because of \r\n present in string,
first replace that string with blank and then check length.
str_string1.Replace("\r\n","")

Thanks.

Convert.ToDateTime(“2019/12/12”)

I usually use that and it works for me