|
-
Jan 21st, 2006, 05:36 PM
#1
Thread Starter
New Member
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 ?
-
Jan 22nd, 2006, 03:59 AM
#2
Thread Starter
New Member
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!
-
Jan 22nd, 2006, 04:07 AM
#3
Hyperactive Member
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)
-
Jan 22nd, 2006, 04:30 AM
#4
Thread Starter
New Member
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.
-
Jan 23rd, 2006, 07:21 AM
#5
Hyperactive Member
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());
-
Jan 23rd, 2006, 07:59 AM
#6
Hyperactive Member
Re: DateTime.Parse
Do you know how to find all classes that implement the IFormatable interface?
-
Jan 24th, 2006, 01:14 AM
#7
Thread Starter
New Member
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!
-
Jan 24th, 2006, 02:13 AM
#8
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.
-
Jan 28th, 2006, 11:18 AM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|