How to parse a string to datetime without a time

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

    How to parse a string to datetime without a time

    Hi,

    I have a string with this value 07/11/2008. As you see no time included.

    How can I parse this string to a datetime variable? I tried to do this but I
    get the exception that the format is incorrect.

    Any ideas?

    I tried both convert... and datetime.par...

    Thanks again!

  • Peter Duniho

    #2
    Re: How to parse a string to datetime without a time

    On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <boah123@hotmai l.comwrote:
    I have a string with this value 07/11/2008. As you see no time included.
    >
    How can I parse this string to a datetime variable? I tried to do this
    but I get the exception that the format is incorrect.
    Did you try the ParseExact() method? I would expect that to work fine.

    Comment

    • Arjen

      #3
      Re: How to parse a string to datetime without a time


      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chreef in bericht
      news:op.udqd1re q8jd0ej@petes-computer.local. ..
      On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <boah123@hotmai l.comwrote:
      >
      >I have a string with this value 07/11/2008. As you see no time included.
      >>
      >How can I parse this string to a datetime variable? I tried to do this
      >but I get the exception that the format is incorrect.
      >
      Did you try the ParseExact() method? I would expect that to work fine.
      I tried this but it did not work.
      DateTime.ParseE xact(Request.Fo rm["date"], @"MM\/dd\/YYYY", null);

      Comment

      • =?UTF-8?B?QXJuZSBWYWpow7hq?=

        #4
        Re: How to parse a string to datetime without a time

        Arjen wrote:
        "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chreef in bericht
        news:op.udqd1re q8jd0ej@petes-computer.local. ..
        >On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <boah123@hotmai l.comwrote:
        >>
        >>I have a string with this value 07/11/2008. As you see no time included.
        >>>
        >>How can I parse this string to a datetime variable? I tried to do
        >>this but I get the exception that the format is incorrect.
        >>
        >Did you try the ParseExact() method? I would expect that to work fine.
        >
        I tried this but it did not work.
        DateTime.ParseE xact(Request.Fo rm["date"], @"MM\/dd\/YYYY", null);
        Try with yyyy instead of YYYY.

        Arne

        Comment

        • Peter Duniho

          #5
          Re: How to parse a string to datetime without a time

          On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <boah123@hotmai l.comwrote:
          I tried this but it did not work.
          DateTime.ParseE xact(Request.Fo rm["date"], @"MM\/dd\/YYYY", null);
          No, of course that wouldn't. :) First, you are trying to escape
          characters in a string that you've declared as an unescaped literal.
          Second, you need lower-case y's, not upper-case.

          Try:

          DateTime.ParseE xact(Request.Fo rm["date"], "MM/dd/yyyy", null);

          Note that since a forward slash is not a special string formatting
          character, you don't need escaping _or_ the use of the @ symbol for the
          string.

          Pete

          Comment

          • =?UTF-8?B?QXJuZSBWYWpow7hq?=

            #6
            Re: How to parse a string to datetime without a time

            Peter Duniho wrote:
            Note that since a forward slash is not a special string formatting
            character,
            It is a special DateTime formatting character !

            Arne

            Comment

            • Arjen

              #7
              Re: How to parse a string to datetime without a time


              "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chreef in bericht
              news:op.udqfmmv f8jd0ej@petes-computer.local. ..
              On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <boah123@hotmai l.comwrote:
              >
              >I tried this but it did not work.
              >DateTime.Parse Exact(Request.F orm["date"], @"MM\/dd\/YYYY", null);
              >
              No, of course that wouldn't. :) First, you are trying to escape
              characters in a string that you've declared as an unescaped literal.
              Second, you need lower-case y's, not upper-case.
              >
              Try:
              >
              DateTime.ParseE xact(Request.Fo rm["date"], "MM/dd/yyyy", null);
              >
              Note that since a forward slash is not a special string formatting
              character, you don't need escaping _or_ the use of the @ symbol for the
              string.
              >
              Pete

              Hmm... maybe I'm blind... I still get the error message. :(

              string data = "07/17/2008";

              DateTime date = DateTime.ParseE xact(data, "MM/dd/yyyy", null);

              Response.Write( date);

              Comment

              • =?UTF-8?B?QXJuZSBWYWpow7hq?=

                #8
                Re: How to parse a string to datetime without a time

                Arjen wrote:
                "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chreef in bericht
                news:op.udqfmmv f8jd0ej@petes-computer.local. ..
                >On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <boah123@hotmai l.comwrote:
                >>
                >>I tried this but it did not work.
                >>DateTime.Pars eExact(Request. Form["date"], @"MM\/dd\/YYYY", null);
                >>
                >No, of course that wouldn't. :) First, you are trying to escape
                >characters in a string that you've declared as an unescaped literal.
                >Second, you need lower-case y's, not upper-case.
                >>
                >Try:
                >>
                > DateTime.ParseE xact(Request.Fo rm["date"], "MM/dd/yyyy", null);
                >>
                >Note that since a forward slash is not a special string formatting
                >character, you don't need escaping _or_ the use of the @ symbol for
                >the string.
                >
                Hmm... maybe I'm blind... I still get the error message. :(
                >
                string data = "07/17/2008";
                >
                DateTime date = DateTime.ParseE xact(data, "MM/dd/yyyy", null);
                >
                Response.Write( date);
                Keep the escaping.

                / in a DateTime format string means current date separator and
                may not be '/' - with my settings it is '-'.

                Arne

                Comment

                • Arjen

                  #9
                  Re: How to parse a string to datetime without a time


                  "Arne Vajhøj" <arne@vajhoej.d kschreef in bericht
                  news:486d3333$0 $90265$14726298 @news.sunsite.d k...
                  Peter Duniho wrote:
                  >Note that since a forward slash is not a special string formatting
                  >character,
                  >
                  It is a special DateTime formatting character !
                  >
                  Arne
                  Aha! Thanks... now it works. :)

                  Comment

                  • Peter Duniho

                    #10
                    Re: How to parse a string to datetime without a time

                    On Thu, 03 Jul 2008 13:19:38 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
                    > Hmm... maybe I'm blind... I still get the error message. :(
                    > string data = "07/17/2008";
                    > DateTime date = DateTime.ParseE xact(data, "MM/dd/yyyy", null);
                    > Response.Write( date);
                    >
                    Keep the escaping.
                    And the @ (the string won't compile otherwise). Alternatively, use the
                    invariant culture instead of "null".

                    Sorry, I overlooked the issue Arne's talking about, as my own system
                    settings use / for the date separator.

                    Pete

                    Comment

                    • Peter Duniho

                      #11
                      Re: How to parse a string to datetime without a time

                      On Thu, 03 Jul 2008 13:14:52 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
                      Peter Duniho wrote:
                      >Note that since a forward slash is not a special string formatting
                      >character,
                      >
                      It is a special DateTime formatting character !
                      Ah, right. Good point.

                      Comment

                      • qglyirnyfgfo@mailinator.com

                        #12
                        Re: How to parse a string to datetime without a time

                        Sorry for the stupid question but….

                        I have never seen this date time formatting character. I searched the
                        date time formatting strings and was not able to find anything
                        regarding “\”.

                        I am kind of lost here, what exactly does having the “\”character
                        does?

                        Thanks.




                        On Jul 3, 3:14 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                        Peter Duniho wrote:
                        Note that since a forward slash is not a special string formatting
                        character,
                        >
                        It is a special DateTime formatting character !
                        >
                        Arne

                        Comment

                        • =?windows-1252?Q?Arne_Vajh=F8j?=

                          #13
                          Re: How to parse a string to datetime without a time

                          qglyirnyfgfo@ma ilinator.com wrote:
                          On Jul 3, 3:14 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                          >Peter Duniho wrote:
                          >>Note that since a forward slash is not a special string formatting
                          >>character,
                          >It is a special DateTime formatting character !
                          Sorry for the stupid question but….
                          >
                          I have never seen this date time formatting character. I searched the
                          date time formatting strings and was not able to find anything
                          regarding “\”.
                          >
                          I am kind of lost here, what exactly does having the “\”character
                          does?
                          We were discussing forward slash not back slash.

                          But both has a special meaning.

                          And both are listed at:

                          Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.


                          Arne

                          Comment

                          • Peter Duniho

                            #14
                            Re: How to parse a string to datetime without a time

                            On Thu, 03 Jul 2008 14:46:39 -0700, <qglyirnyfgfo@m ailinator.comwr ote:
                            Sorry for the stupid question but….
                            >
                            I have never seen this date time formatting character. I searched the
                            date time formatting strings and was not able to find anything
                            regarding “\”.
                            >
                            I am kind of lost here, what exactly does having the “\”characte r
                            does?
                            It escapes the forward slash so that the DateTime parser knows to expect
                            an exact match in the input string, rather than treating the forward slash
                            as the special character that it is (as Arne pointed out :) ).

                            Without the escaping, the parser will treat the forward slash as a
                            placeholder representing whatever the current culture's date separator
                            character is, which may or may not actually be a forward slash (on English
                            systems it likely would be, but for other cultures, it could be something
                            else, like a hyphen or a period, for example).

                            Pete

                            Comment

                            • Cor Ligthert[MVP]

                              #15
                              Re: How to parse a string to datetime without a time

                              Peter,

                              (on English
                              systems it likely would be, but for other cultures, it could be something
                              else, like a hyphen or a period, for example).
                              >
                              You probably mean (on US systems........ .........?

                              AFAIK is by instance the UK confirm the most used European behaviour in this
                              (this excluding countries where the ISO 8601 in Europe is used like by
                              instance Sweden and Russia)

                              While the rest of the English speaking world including Canada uses the UK
                              system.

                              Although we are in Europe not very consistent in this, sometimes we use a
                              slash, a hyphen, a dot or just a space.

                              Cor

                              Comment

                              Working...