How to convert string to date time?

Hello,

I have a string as 1012022 in my excel sheet and I want to convert this into date time format.

Date.ParseExact(RegRow(“DAY”).Tostring.Trim,“dMMyyyy”,System.Globalization.CultureInfo.InvariantCulture)

where Day=“1012022”

I had tried date parsing and convert.datetime methods but they didn’t worked.
Could any one please help me on this.

Thanks in advance.

HI @Sirimalla_Karthik_Chandra

Check with the below expression below would be the Expression!

DateTime.ParseExact(RegRow(“DAY”).Tostring.Substring(0,7),“dMMyyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd-MM-yyyy”)

If the above not working share the Column Printing Using the write line

Regards

Hi,

Can you try the following expression?

DateTime.ParseExact(RegRow("DAY").Tostring.Trim.PadLeft(8, "0"c),"ddMMyyyy",System.Globalization.CultureInfo.InvariantCulture)

Regards,

Hi @Sirimalla_Karthik_Chandra ,
TRY BELO CODE

DateTime.ParseExact("1012022","ddMyyy",System.Globalization.CultureInfo.InvariantCulture).ToString("dd-MM-yy")

Regards,
Arivu

Hi @Sirimalla_Karthik_Chandra

It will work with either Date.ParseExact/DateTime.ParseExact

Thank you.

1 Like

Hi @Jobin_Joy , thanks for replying to my query.
The String “1012022” should refer to the date 01-01-2022.

Hi @arivu96 thanks for replying. The String “1012022” should refer to the date 01-01-2022.

Hi @Yoichi, Thanks man for helping me out. Your Expression is working for me.

1 Like

Hi @Sirimalla_Karthik_Chandra

This technically an incorrect date format. Is there any specific reason for this format?

Because in some point of time the above format will fail.

ex: 11th Dec 2021 → (11122021)how will you indicate this date in the above format? (dMMyyyy)
single digit day is not sufficient right.

Thank you.

I accept your point that the date format is technically incorrect. I only get dates from 1 to 9 in single digit followed by month and year. However After 9 the dates are generated in standard format that is in “10012022”, Hence I’m not facing any issues for dates in between “10” to “31” while date parsing.

The above expression provided by @Yoichi is working fine for me.

Thank you so much @Jobin_Joy for replying to my query and helping me as much as possible.

Got it. Happy Automation :slight_smile:

1 Like

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