Results 1 to 9 of 9

Thread: DateTime.Parse

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    5

    DateTime.Parse

    Hello all,

    First post. So I'll try and make it a good one

    I started with C# earlier today having dabbled with VB4, ANSI C, C++, PHP, Java etc etc. I've got a string which I'm trying to parse as a date using DateTime.Prase ( I know, I should Use DateTime.ParseExact., but I'd just like to get it working before improving my code :P).

    I'm storing two dates is a text file, encrypted using System.Security.Cryptography (Wow! - How cool is that? .Net makes it soo easy ), these dates are then being decrypted and compared. Basically, DateTime doesn't like one of them as a date, as you can see below:

    Decrypted key:21/01/2006 22:28:02|21/01/2007 22:28:02

    Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
    in <0x00121> System.DateTime:Parse (System.String s, IFormatProvider fp, DateTimeStyles styles)
    in <0x0002c> System.DateTime:Parse (System.String s, IFormatProvider fp)
    in <0x00022> System.DateTime:Parse (System.String s)
    in <0x00076> LicenseKey:get_ValidFrom ()
    in <0x000a6> ProductRegistration:Main (System.String[] args)
    And, here's the important bit of code which gets the first date:

    Code:
     	 public DateTime ValidFrom
     	 {
     	 	get
     	 	{
    			string KeyData			=	this.Decode(this.Raw);	
    			string[] KeyArray;
    			KeyArray				=	System.Text.RegularExpressions.Regex.Split(KeyData, "\\|");
    			return DateTime.Parse( KeyArray[0] );
     		}
     	 }
    There's then an identical function below which gets the second date. When I try and access these, well, as you've already seen. It doesn't work. Any ideas ?

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    5

    Re: DateTime.Parse

    Right. Will try and follow up my own post

    It seems the problem occurs because I'm storing dates in en-GB format (dd/mm/yyyy hh:mm:ss) and yet, ParseTime is trying do use US format (mm/dd/yyyy hh:mm:ss)?

    So, I'll have to use DateTime.ParseExact. However, with my attempts I keep getting:
    Unhandled Exception: System.FormatException: Invalid format.
    Could someone perhaps provide an example of how I'd make DateTime.Parse or DateTime.ParseExact understand en-GB date formats ?

    Thanks!

  3. #3
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Re: DateTime.Parse

    it's because the date is not in the current windows date format.

    You could either change your date options in the control panel to be DD/MM/YYYY format, swap M the D, or maybe try some of the options for parsing (maybe the IFormatProver param would help)

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    5

    Re: DateTime.Parse

    I kinda follow.

    I've switched to using DateTIme.Parse with Globalization, as so:
    Code:
    			System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");
    			return DateTime.Parse("21/01/2006 22:28:02", culture, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
    Alas, i'm still getting:
    Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
    So. How would I go about introducting IFromatProvider into that ?

    Thanks.

  5. #5
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Re: DateTime.Parse

    it seems you already did.

    If I do this, there are no errors, and it comes back to my windows format of m/d/yy.

    Code:
    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");
    MessageBox.Show(DateTime.Parse("21/01/2006 22:28:02", culture).ToString());

  6. #6
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Re: DateTime.Parse

    Do you know how to find all classes that implement the IFormatable interface?

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    5

    Re: DateTime.Parse

    Don't get chance to do C# work during the week due to work. Will have to wait until the weekend to try it out. Thanks!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DateTime.Parse

    If you know exactly what the format will be then you can use ParseExact. It will let you specify formats that Parse can't understand, e.g. "yyyyMMddHHmmss", which is a good name for files if you want "alphabetical" order to match chronological order.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    5

    Re: DateTime.Parse

    Thanks guys.

    I ended up just using DateTime.UtcNow , it was the quickest and easiest way of doing what I wanted

Posting Permissions

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



Click Here to Expand Forum to Full Width