Date Format

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

    Date Format

    Hi,

    I need to convert a string into DateTime. I can use Convert.ToDateT ime()
    however the string can be in different formats like 19700504 or 05041970.
    Any ideas?

    Thanks


  • Michael Nemtsev

    #2
    Re: Date Format

    Hello Bob,

    One of the parameters of .ToDataTime is IFormatProvider where you can specify
    the format of your string
    See sample in MSDN

    BHi,
    B>
    BI need to convert a string into DateTime. I can use
    BConvert.ToDate Time() however the string can be in different formats
    Blike 19700504 or 05041970. Any ideas?
    B>
    BThanks
    B>
    ---
    WBR,
    Michael Nemtsev :: blog: http://spaces.live.com/laflour

    "At times one remains faithful to a cause only because its opponents do not
    cease to be insipid." (c) Friedrich Nietzsche


    Comment

    • james.curran@gmail.com

      #3
      Re: Date Format

      ..ToString("YYY YMMDD") or .ToString("MMDD YYYY");

      (You might want tolook that up, as I can never remember if capital M is
      months or minutes.)

      Bob wrote:
      >
      I need to convert a string into DateTime. I can use Convert.ToDateT ime()
      however the string can be in different formats like 19700504 or 05041970.
      Any ideas?

      Comment

      • Mark Rae

        #4
        Re: Date Format

        <james.curran@g mail.comwrote in message
        news:1159981738 .186226.111660@ h48g2000cwc.goo glegroups.com.. .
        (You might want tolook that up, as I can never remember if capital M is
        months or minutes.)
        Capital M is months


        Comment

        • John B

          #5
          Re: Date Format

          Bob wrote:
          Hi,
          >
          I need to convert a string into DateTime. I can use Convert.ToDateT ime()
          however the string can be in different formats like 19700504 or 05041970.
          Any ideas?
          >
          Thanks
          >
          >
          Check out DateTime.ParseE xact
          You can pass a format string that will be used for parsing the date.
          JB

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Date Format

            Bob,

            That wont work with convert, use the datetime.parsee xact for that, than you
            can tell exact what part of your string is a datetime element.

            Cor

            "Bob" <robert@bob.com schreef in bericht
            news:%23N2sRU95 GHA.3952@TK2MSF TNGP04.phx.gbl. ..
            Hi,
            >
            I need to convert a string into DateTime. I can use Convert.ToDateT ime()
            however the string can be in different formats like 19700504 or 05041970.
            Any ideas?
            >
            Thanks
            >

            Comment

            • Bob

              #7
              Re: Date Format

              Thanks for all the posts. DateTime.ParseE xact works great however I have a
              problem. I get passed different date formats like 19700504 or 05041970.
              How can I handle different date formats?

              Thanks

              "John B" <jbngspam@yahoo .comwrote in message
              news:45246f39$1 _1@news.iprimus .com.au...
              Bob wrote:
              >Hi,
              >>
              >I need to convert a string into DateTime. I can use Convert.ToDateT ime()
              >however the string can be in different formats like 19700504 or 05041970.
              >Any ideas?
              >>
              >Thanks
              Check out DateTime.ParseE xact
              You can pass a format string that will be used for parsing the date.
              JB

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Date Format

                Bob,

                That are no date format, that are strings of numbers.

                Cor

                "Bob" <robert@bob.com schreef in bericht
                news:%239aKroD6 GHA.4616@TK2MSF TNGP05.phx.gbl. ..
                Thanks for all the posts. DateTime.ParseE xact works great however I have
                a problem. I get passed different date formats like 19700504 or 05041970.
                How can I handle different date formats?
                >
                Thanks
                >
                "John B" <jbngspam@yahoo .comwrote in message
                news:45246f39$1 _1@news.iprimus .com.au...
                >Bob wrote:
                >>Hi,
                >>>
                >>I need to convert a string into DateTime. I can use
                >>Convert.ToDat eTime() however the string can be in different formats like
                >>19700504 or 05041970. Any ideas?
                >>>
                >>Thanks
                >Check out DateTime.ParseE xact
                >You can pass a format string that will be used for parsing the date.
                >JB
                >
                >

                Comment

                • Mark Rae

                  #9
                  Re: Date Format

                  "Bob" <robert@bob.com wrote in message
                  news:%239aKroD6 GHA.4616@TK2MSF TNGP05.phx.gbl. ..
                  Thanks for all the posts. DateTime.ParseE xact works great however I have
                  a problem. I get passed different date formats like 19700504 or 05041970.
                  How can I handle different date formats?
                  In exactly the same way...

                  DateTime dtm1 = DateTime.ParseE xact("19700504" , "yyyyMMdd", null);
                  DateTime dtm2 = DateTime.ParseE xact("05041970" , "MMddyyyy", null);


                  Comment

                  • Bill Butler

                    #10
                    Re: Date Format


                    "Bob" <robert@bob.com wrote in message
                    news:%239aKroD6 GHA.4616@TK2MSF TNGP05.phx.gbl. ..
                    Thanks for all the posts. DateTime.ParseE xact works great however I
                    have a problem. I get passed different date formats like 19700504 or
                    05041970. How can I handle different date formats?
                    >
                    Thanks
                    >
                    "John B" <jbngspam@yahoo .comwrote in message
                    news:45246f39$1 _1@news.iprimus .com.au...
                    >Bob wrote:
                    >>Hi,
                    >>>
                    >>I need to convert a string into DateTime. I can use
                    >>Convert.ToDat eTime() however the string can be in different formats
                    >>like 19700504 or 05041970. Any ideas?
                    Since you can get in data with either form, you are going to have to
                    validate it yourself.
                    quick and dirty way (assuming you are looking at recent dates and not
                    historical dates)

                    - convert the first two characters to an integer.
                    - if it is greater than 12 then the year is first
                    string date = "19700504";
                    int x = Convert.ToInt32 (date.Substring (0,2));
                    if (x 12)
                    Console.WriteLi ne("{0} is in yyyyMMdd format", date);
                    else
                    Console.WriteLi ne("{0} is in MMddyyyy format", date);

                    Bill


                    Comment

                    Working...