PDA

Click to See Complete Forum and Search --> : DateTime.Parse


DaveHope
Jan 21st, 2006, 04:36 PM
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:

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 ? :)

DaveHope
Jan 22nd, 2006, 02:59 AM
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!

wordracr
Jan 22nd, 2006, 03:07 AM
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)

DaveHope
Jan 22nd, 2006, 03:30 AM
I kinda follow.

I've switched to using DateTIme.Parse with Globalization, as so:
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.

wordracr
Jan 23rd, 2006, 06:21 AM
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.


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

wordracr
Jan 23rd, 2006, 06:59 AM
Do you know how to find all classes that implement the IFormatable interface?

DaveHope
Jan 24th, 2006, 12:14 AM
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! :)

jmcilhinney
Jan 24th, 2006, 01:13 AM
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.

DaveHope
Jan 28th, 2006, 10:18 AM
Thanks guys.

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