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. Managed C++/CLI
  4. DateTime->Parse

DateTime->Parse

Scheduled Pinned Locked Moved Managed C++/CLI
jsonhelpquestion
5 Posts 2 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.
  • V Offline
    V Offline
    VonHagNDaz
    wrote on last edited by
    #1

    Hey Guys, I'm having a little problem parsing a date and time from a string.

    		DateTime^ start = gcnew DateTime();
    
    		temp = "";
    		this->thisJob->getStartDate(temp);
    		start->Parse(temp);
    

    temp is "5/16/2011" however, after start->parse(temp) the value of start is always "1/1/1 0:0:0" I'm only interested in the date "5/26/2011". MSDN has several differnt "Parse" methods, and I've tried a few others, but the result is always "1/1/1 0:0:0" Sorry for the smileys, I think CP website is taking my returned string I'm typing into the question and converting it into some kind of smiley code.

    [Insert Witty Sig Here]

    M 1 Reply Last reply
    0
    • V VonHagNDaz

      Hey Guys, I'm having a little problem parsing a date and time from a string.

      		DateTime^ start = gcnew DateTime();
      
      		temp = "";
      		this->thisJob->getStartDate(temp);
      		start->Parse(temp);
      

      temp is "5/16/2011" however, after start->parse(temp) the value of start is always "1/1/1 0:0:0" I'm only interested in the date "5/26/2011". MSDN has several differnt "Parse" methods, and I've tried a few others, but the result is always "1/1/1 0:0:0" Sorry for the smileys, I think CP website is taking my returned string I'm typing into the question and converting it into some kind of smiley code.

      [Insert Witty Sig Here]

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Try something like this...

      String ^temp = "5/26/2011";
      DateTime start;
      if (DateTime::TryParse(temp, start))
      {
          // do something with start
      }
      

      ...or this...

      String ^temp = "5/26/2011";
      try
      {
          DateTime start = DateTime::Parse(temp);
      
          // do something with start
      }
      catch (Exception ^)
      {
      }
      

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      modified on Monday, May 16, 2011 2:32 PM

      V 1 Reply Last reply
      0
      • M Mark Salsbery

        Try something like this...

        String ^temp = "5/26/2011";
        DateTime start;
        if (DateTime::TryParse(temp, start))
        {
            // do something with start
        }
        

        ...or this...

        String ^temp = "5/26/2011";
        try
        {
            DateTime start = DateTime::Parse(temp);
        
            // do something with start
        }
        catch (Exception ^)
        {
        }
        

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        modified on Monday, May 16, 2011 2:32 PM

        V Offline
        V Offline
        VonHagNDaz
        wrote on last edited by
        #3

        That works! I'm a bit confused as to why your code works and mine doesn't. What is the difference between SomeDateTimeObject.Parse, and DateTime::Parse?

        [Insert Witty Sig Here]

        M 1 Reply Last reply
        0
        • V VonHagNDaz

          That works! I'm a bit confused as to why your code works and mine doesn't. What is the difference between SomeDateTimeObject.Parse, and DateTime::Parse?

          [Insert Witty Sig Here]

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          All the Parse() methods are static (AFAIK) so they return a new DateTime object instead of altering the one you were calling Parse() on. This would have worked;

          start = start->Parse(temp);
          

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          V 1 Reply Last reply
          0
          • M Mark Salsbery

            All the Parse() methods are static (AFAIK) so they return a new DateTime object instead of altering the one you were calling Parse() on. This would have worked;

            start = start->Parse(temp);
            

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            V Offline
            V Offline
            VonHagNDaz
            wrote on last edited by
            #5

            I see, MSDN wasn't too clear on that. It lead me to believe temp would be modified. Thank you, 5's for both.

            [Insert Witty Sig Here]

            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