Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    47
    Thanks
    2
    Thanked 1 Time in 1 Post

    DateTime.ParseExact Problem

    Can someone please tell me what I'm doing wrong:

    I have this string: 11/24/2006 03:30

    That I am trying to put in the following format: 2006-11-24 03:30 AM

    I tried using Parse Exact:

    datetime.parseExact('11-24-2006 03:30','yyyy-MM-dd HH:mm tt*',CultureInfo)

    That generates the following error:

    System.FormatException: String was not recognized as a valid DateTime.

    Can someone please tell me what I am doing wrong. Is the string format wrong?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    ParseExact means the string is already IN that format and you want a date out of it. If you want to convert a string that is in one format into a different format, you make a date first, then format that date.

    i.e. (C#)
    Code:
    string origDateString = "11-24-2006 03:30";
    string desiredFormat = "yyyy-MM-dd HH:mm tt*";
    
    DateTime datetime = DateTime.Parse(origDateString);
    string convertedDateString = datetime.toString(desiredFormat);
    You could shorten that up more and pass culture arguments etc. if you had a mind to, but you get the idea. I also generally use TryParse when playing with date parsing, but that's a 2.0 thing so it won't work with 1.1.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    47
    Thanks
    2
    Thanked 1 Time in 1 Post
    Thanks Nikki,

    I think I'm going in the right direction at least now. The only problem now is that the date is coming back like this: 0000-00-00 00:00. Eventhough, the original string is 2006-11-24 23:59.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Can you post the code?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New Coder
    Join Date
    Nov 2006
    Posts
    47
    Thanks
    2
    Thanked 1 Time in 1 Post
    I figured out the problem. It wasn't really with the code, there was a problem in Mysql once I tried inserting the value. The format had to be in yyyy-mm-dd H:mm (24 hour clock) and there was a space between the time and the AM and PM (which really didn't matter since MySql stripped the AM/PM anyway) which was causing the initial error.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •