Convert string to datetime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    #1

    Convert string to datetime

    I have read through various posts on converting a string into the datetime format, but cannot resolve my situation. I have a string in the format of 'yyyymmddthhmms s.sssz' that I would like to convert to datetime. My previous attempts:

    DateTime dt = DateTime.Parse( string);
    DateTime dt = Convert.ToDateT ime(string);
    DateTime dt = XmlConvert.ToDa teTime(string, format); where format = 'yyyymmddthhmms s.sssz'

    I should note that the string value is being extracted from an Xml document. Any help would be appreciated.
  • Lacutas
    New Member
    • Jan 2008
    • 5

    #2
    Originally posted by mcfly1204
    I have read through various posts on converting a string into the datetime format, but cannot resolve my situation. I have a string in the format of 'yyyymmddthhmms s.sssz' that I would like to convert to datetime. My previous attempts:

    DateTime dt = DateTime.Parse( string);
    DateTime dt = Convert.ToDateT ime(string);
    DateTime dt = XmlConvert.ToDa teTime(string, format); where format = 'yyyymmddthhmms s.sssz'

    I should note that the string value is being extracted from an Xml document. Any help would be appreciated.
    Just a quick thought, are you removing the surround tag elements from your string before you try to parse it?

    Comment

    • mcfly1204
      New Member
      • Jul 2007
      • 233

      #3
      Originally posted by Lacutas
      Just a quick thought, are you removing the surround tag elements from your string before you try to parse it?
      I am using XmlResolver.Cre ateNavigator to extract string values out via the xpath, so only the contents of the given tag are being extracted, not the tag in its entirety.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I would recomend DateTime.ParseE xact() (Normally I would say .Parse() or .TryParse() but it didn't look like they supported custom formats like that)

        Code:
        // "20080129P015750.100-5" <-you cannot get better format then that?
        string format="yyyyMMddthhmmss.sssz";
        //NOTE: I had to fix your format, as you had "2 digit minutes" specified in there twice
        DateTime dt = DateTime.ParseExact(string, format, <some format provider>);
        Have to check in on what format provider you wanna use, msdn should help.

        Comment

        • mcfly1204
          New Member
          • Jul 2007
          • 233

          #5
          Originally posted by Plater
          // "20080129P01575 0.100-5" <-you cannot get better format then that?
          Unfortunately this is someone's idea of a "standard".

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Did correcting the format string have any effect?
            You could always write your own too if need be.

            Comment

            • mcfly1204
              New Member
              • Jul 2007
              • 233

              #7
              Originally posted by Plater
              Did correcting the format string have any effect?
              You could always write your own too if need be.
              It kept saying that the string was not a valid date format. I broke down and removed the extra characters so that it was simply 'yyyyMMddHHmmss '. Below did not work for me.

              string = "20071026T12000 0.000Z";
              IFormatProvider US_Format = new System.Globaliz ation.CultureIn fo("en-US", true);
              string format = "yyyyMMddTHHmms s".sssZ;
              RefDateTimeStam p = DateTime.ParseE xact(string, format, US_Format);

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Oh the Z was a literal and not the designation for gmt offset? The same with the T, it is not the AM/PM designator but ACTUALLY a T?
                What is an example of the exact string you will be parsing?

                EDIT: ok I found more problems with your date format string, and have corrected with the following example:
                Code:
                string s = "20071026A120000.000-5";//example string to parse
                IFormatProvider US_Format = new System.Globalization.CultureInfo("en-US", true);
                string format = "yyyyMMddtHHmmss.fffz";
                DateTime dt = DateTime.ParseExact(s, format, US_Format);
                MessageBox.Show(dt.ToString());

                Comment

                • leoiser
                  New Member
                  • Jul 2007
                  • 41

                  #9
                  Try this

                  DateTime.Now.To String("yyyymmd dthhmmss.fff tt")

                  tt for AM/PM

                  Comment

                  • leoiser
                    New Member
                    • Jul 2007
                    • 41

                    #10
                    string = "20071026T12000 0.000Z";

                    In the above string what is 'T' stands for?

                    If nothing works try the below

                    Dim s As String = "20071026T12000 0.000Z" //Assuming T stands for time
                    s = s.Insert(4, "/")
                    s = s.Insert(7, "/")
                    s = s.Replace("T", " ")
                    s = s.Replace("Z", "")
                    s = s.Insert(13, ":")
                    s = s.Insert(16, ":")

                    MessageBox.Show (Convert.ToDate Time(s))

                    Is it helpful?

                    Originally posted by mcfly1204
                    It kept saying that the string was not a valid date format. I broke down and removed the extra characters so that it was simply 'yyyyMMddHHmmss '. Below did not work for me.

                    string = "20071026T12000 0.000Z";
                    IFormatProvider US_Format = new System.Globaliz ation.CultureIn fo("en-US", true);
                    string format = "yyyyMMddTHHmms s".sssZ;
                    RefDateTimeStam p = DateTime.ParseE xact(string, format, US_Format);

                    Comment

                    Working...