Parsing custom datetime string

  • Thread starter Thread starter Sudeep
  • Start date Start date
S

Sudeep

Hi,

I need to parse following string.
string dt = "Thur 11/2/2006 9:05:52 PM";

DateTime.Parse() or TryParse() fail to parse the given string. The day
in the above string "Thur" is causing the problem . If i change this
to "Thu" it gets parsed. Is there any way i can parse this string
without having to manually change "Thur" to "Thu".

Thanks
Sudeep.
 
Is there any way i can parse this string without having to manually
change "Thur" to "Thu".

You might be able to do this by implementing your own CultureInfo,
specifying all abbreviated names of week days (ddd) with four letter
strings, rather than the usual three letters strings provided by the
Engilsh cultures built into the framework.

However, it might just be a lot easier to simply remove the 4th char in
each date string as you recieve it :)
 
Sudeep said:
I need to parse following string.
string dt = "Thur 11/2/2006 9:05:52 PM";

DateTime.Parse() or TryParse() fail to parse the given string. The day
in the above string "Thur" is causing the problem . If i change this
to "Thu" it gets parsed. Is there any way i can parse this string
without having to manually change "Thur" to "Thu".

Fix whatever is generating that non-standard format.

If that is not possible then:

string s = "Thur 11/02/2006 9:05:52 PM";
CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
DateTimeFormatInfo dtf = (DateTimeFormatInfo)ci.DateTimeFormat.Clone();
dtf.AbbreviatedDayNames = new string[] { "Sund", "Mond", "Tues", "Wedn",
"Thur", "Frid", "Satu" };
dtf.DateSeparator = "/";
dtf.TimeSeparator = ":";
dtf.AMDesignator = "AM";
dtf.PMDesignator = "PM";
ci.DateTimeFormat = dtf;
DateTime dt = DateTime.ParseExact(s, "ddd M/d/yyyy h:mm:ss tt", ci);

Arne
 
Back
Top