feurich Posted January 16, 2006 Posted January 16, 2006 Hi there, I am trying to check if a date string 12-12-2005 is in date format in C#. But I can't seem to find a way of doing it. I just get a string like 12-12-2005 and I want to check if this string is in the regional date format. Thanks, Feurich Quote Trust the Universe
feurich Posted January 16, 2006 Author Posted January 16, 2006 [csharp] private void button1_Click(object sender, System.EventArgs e) { if (CheckIfDate(textBox1.Text)) { MessageBox.Show("Deze string voldoet aan het datum formaat"); } else { MessageBox.Show("Deze string voldoet niet aan het datum formaat"); } } private bool CheckIfDate(string Value) { try { System.DateTime odate = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990,5,6)).ConvertFrom(textBox1.Text)); textBox2.Text = odate.Date.ToString(); return true; } catch { return false; } [/csharp] Quote Trust the Universe
Administrators PlausiblyDamp Posted January 16, 2006 Administrators Posted January 16, 2006 You could also use DateTime.Parse() or DateTime.ParseExact() to try the conversion rather than relying on a TypeDescriptor.GetConverter. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
feurich Posted January 17, 2006 Author Posted January 17, 2006 Thanks Hi PD, Looked at your suggestion and it makes sense. Less code and more clear. Thanks, Feurich Quote Trust the Universe
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.