Parsing custom datetime string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sudeep

    Parsing custom datetime string

    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.
  • Joern Schou-Rode

    #2
    Re: Parsing custom datetime string

    On Sun, 02 Nov 2008 09:05:35 +0100, Sudeep <sudeep.kc@gmai l.comwrote:
    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 :)

    --
    Joern Schou-Rode

    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: Parsing custom datetime string

      Sudeep wrote:
      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)Cu ltureInfo.Curre ntCulture.Clone ();
      DateTimeFormatI nfo dtf = (DateTimeFormat Info)ci.DateTim eFormat.Clone() ;
      dtf.Abbreviated DayNames = new string[] { "Sund", "Mond", "Tues", "Wedn",
      "Thur", "Frid", "Satu" };
      dtf.DateSeparat or = "/";
      dtf.TimeSeparat or = ":";
      dtf.AMDesignato r = "AM";
      dtf.PMDesignato r = "PM";
      ci.DateTimeForm at = dtf;
      DateTime dt = DateTime.ParseE xact(s, "ddd M/d/yyyy h:mm:ss tt", ci);

      Arne

      Comment

      Working...