Problem with datetime.ParseExact()

Hi guys,
I’ve some issues with ParseExact. When I try to insert a array of string instead a string for my format that doesn’t work :cry:

This format work at this moment: datetime.ParseExact(mydate,“dd/MM/yyyy”,Nothing)

So i just want to remplace “dd/MM/yyyy” by a array of string like {“MM/dd/yyyy”,“M/d/yyyy”}

— From .net documentation: ParseExact(String, String, IFormatProvider, DateTimeStyles)

image

1 Like

Hi @Elya

Do you want to enter the date in a specific format like {“MM/dd/yyyy”} or any other format.
Did i understand your issue correctly…?

If so use like this

Datetime.ToString(“MM/dd/yyyy”) or
Datetime.ToString(“M/d/yyyy”) or any format you want, but within tostring()

As you say you pass by array with different format, you can pass that as a string within this ToString() all inside a for each loop.

Cheers.

2 Likes

Hello @Elya you can not pass String to DateTIme.Parse method. There can be work around but its all depends on you processing steps…
we can give you DateTimeObject… but you may or may not need it…

  1. let us know why you want to parse string to datetime objects
  2. what will be your next processing steps etc.
string strDate = "04/23/2019";
string[] arr = {"MM/dd/yyyy","M/d/yyyy"};
DateTime dt = DateTime.ParseExact(strDate, arr, new CultureInfo("en-US"),
                                         DateTimeStyles.None);

Console.WriteLine(dt.ToShortDateString());

The sample C# code works for me.

You must initialize the culture info object. Then only the array of formats will be considered and works.

Regards,
Karthik Byggari

8 Likes

yeah that way is possible but not really clean instead to use one command :upside_down_face:

1 Like

@Palaniyappan @KarthikByggari @AkshaySandhu
Ty guys for your help!

1 Like

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