Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. parsing a string with DateTime.ParseExact

parsing a string with DateTime.ParseExact

Scheduled Pinned Locked Moved C#
helpregexjsonquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dfn
    wrote on last edited by
    #1

    Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.

    C L S D 4 Replies Last reply
    0
    • D dfn

      Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I'd use an overload of the ToString method on the datetime, which allows you to specify the format. This looks like too many steps to take, to me.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • D dfn

        Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, if you want your ParseExact to succeed, today must consist of exactly four digits. So it all depends on your regional settings, which control the outcome of ToShortDateString(). You could try String today = DateTime.Today.Date.ToString("MMdd"); :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


        1 Reply Last reply
        0
        • D dfn

          Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          What exactly are you trying to do? Do you already have a DateTime object that you are trying to display in a specific format? If so, you just need to call ToString on that object and pass the format string you want. The code you show takes a DateTime object, converts it to a string, and then tries to parse that string and convert it back to a DateTime object, which you then call ToString on to turn it back into a string. You can accomplish the same thing by doing this: Console.WriteLine("today's date is: " + DateTime.Today.ToString("MMdd"));

          Scott.


          —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

          1 Reply Last reply
          0
          • D dfn

            Hi. I am having trouble parsing a string into the format I want. Basically, I want the string to end up being "MMdd" where MM is the month (with a leading zero if necessary) and dd is the day (with a leading zero if necessary). This is what I have tried: String today = DateTime.Today.Date.ToShortDateString(); String pattern = "MMdd"; DateTime dt_today = DateTime.ParseExact(today, pattern, null); // line 3 today = dt_today.ToString(); Console.WriteLine("today's date is: " + today); The problem is that my program crashes once run. If you look at the third line, in place of null, I have tried sending: System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CultureInfo.CurrentCulture Both yielded the same result. I have also tried using "Now" instead of "Today" in the first line. What am I doing wrong? I'd appreciate any help.

            D Offline
            D Offline
            dfn
            wrote on last edited by
            #5

            Thanks all for the replies! I ended up using the overloaded ToString method.

            A 1 Reply Last reply
            0
            • D dfn

              Thanks all for the replies! I ended up using the overloaded ToString method.

              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #6

              I like how all the replies say the same thing :laugh:

              My current favourite word is: Waffle Cheese is still good though.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups