jcrcarmo Posted December 18, 2005 Posted December 18, 2005 Hi folks, I'm having trouble converting text to date and comparing them. Is there a similar to VB.Net's IsDate in C# 2005? Please check out the code below and see if you can find what's wrong. Yeah, I know, I'm a newbie... :) The code in red is where I'm getting the error messages: -------------------------------------------------------------------------- 1) Checking if the text of a TextBox is date: if IsDate(this.Date1TextBox.Text) { } -------------------------------------------------------------------------- 2) Converting and comparing the dates of two TextBoxes: if (Convert.ChangeType(this.Date1TextBox.Text, DateTime) > Convert.ChangeType(this.Date2TextBox.Text, DateTime)) { } -------------------------------------------------------------------------- Thanks a lot, JC :) Quote
Cags Posted December 18, 2005 Posted December 18, 2005 1) Checking if the text of a TextBox is date: if IsDate(this.Date1TextBox.Text) { } One method for doing this is something along the lines of... public bool IsDate(object inValue) { bool bValid; try { DateTime myDT = DateTime.Parse(inValue); bValid = true; } catch (FormatException e) { bValid = false; } return bValid; } 2) Converting and comparing the dates of two TextBoxes: if (Convert.ChangeType(this.Date1TextBox.Text, DateTime) > Convert.ChangeType(this.Date2TextBox.Text, DateTime)) { } That throws an error because the overload requires a type and your passing it an object. You could change the DateTime to typeof(DateTime), don't know if that would help though I haven't tried it. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted December 18, 2005 Leaders Posted December 18, 2005 (edited) Mmmmm.... code.... /* TryParse tries to parse a string. If it succeeds, it stores * the date in the OUT variable and returns true. If it fails, it * returns false. */ if(DateTime.TryParse(MyDateString, out MyDate)) { // > and < operators are overloaded. if(MyDate > DateTime.Now) { // It's the future! } } P.S. Not to rag on you, Cags, but I would personally discourage using code like the code you posted: public bool IsDate(object inValue) { bool bValid; try { DateTime myDT = DateTime.Parse(inValue); bValid = true; } catch (FormatException e) { bValid = false; } return bValid; } It essentially depends on exception handling for validation, and exception handling is specifically not intended to be used for program flow control, but rather for "exceptional circumstances" (and that's not exceptional in a good way, either). Edited December 19, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
jcrcarmo Posted December 19, 2005 Author Posted December 19, 2005 Hi Cags, I tried your suggestion, but unfortunately it didn't work. Thanks for the quick reply anyway! Bye for now, JC :) Quote
jcrcarmo Posted December 19, 2005 Author Posted December 19, 2005 Hi MarbleEater, your suggestion works! Thanks a lot for the reply. Bye for now, JC :) Quote
Cags Posted December 19, 2005 Posted December 19, 2005 P.S. Not to rag on you' date=' Cags, but I would personally discourage using code like the code you posted:[/quote'] Not a problem I didn't like this method but it was the only one I could think of. Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.