String to Date converter

Hi Team,

I am extracting a date in string format say Jan 1, 2022
Conversion should work even if the date is Jan 10, 2022
I want to convert to Date time for which ever the date comes 1 single digit or 10 two digits
Current Code
DateTime.ParseExact(Test,“MMM d, yyyy hh:mm tt”, System.Globalization.CultureInfo.InvariantCulture)

Hi @Robotics

How about this expression?

DateTime.ParseExact("Jan 01, 2022",{"MMM dd, yyyy","MMM d, yyyy","MMM d, yyyy hh:mm tt","MMM dd, yyyy hh:mmtt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("MMM dd, yyyy")

image

Regards
Gokul

Hello Robotics, :smiley:

This way you are doing it works! Delete “hh:mm tt” of your code…

DateTime.ParseExact(Test,“MMM d, yyyy”, System.Globalization.CultureInfo.InvariantCulture)

Convert.xaml (6.7 KB)

Thanks for your time Gokul.
Date i am extracting is Oct 6, 2022 2:55PM
Placed your code in assign storing the result in a Datetime variable. Still the same error
String was not recognized as valid datetime

HI @Robotics

Try with this

DateTime.ParseExact("Jan 01, 2022",{"MMM dd, yyyy","MMM d, yyyy","MMM d, yyyy hh:mmtt","MMM dd, yyyy hh:mmtt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("MMM dd, yyyy")

Hi @Robotics try this one:

  1. strDate=“Oct 6, 2022 2:55PM”

  2. DateTime.ParseExact(strDate,“MMM d, yyyy h:mmtt”, CultureInfo.InvariantCulture).ToString(“MM/dd/yyyy”)

Not working storing result in datetime variable
2:55 PM changed the code to h:mm tt",

Still the same error

Can you share the Input string?

have you try with below express @Robotics

DateTime.ParseExact("Jan 01, 2022",{"MMM dd, yyyy","MMM d, yyyy","MMM d, yyyy hh:mm tt","MMM d, yyyy hh:mmtt","MMM dd, yyyy hh:mm tt","MMM dd, yyyy hh:mmtt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("MMM dd, yyyy")

Regards
Gokul

@Robotics okay check this code.
Sequence1.xaml (6.3 KB)