Formatting a date String

feurich

Centurion
Joined
Oct 21, 2003
Messages
170
Location
Holland
Hi there,

I am using th DateTime.Parse function to check if a datestring is valid. But when I type in "12", this is also validated as a valid datestring.
the only datestrings that i want to be validated true are
1-1-2005, 01-9-2005, 31-12-2005, all others like 12-2005 or 07-05 for me are not valid dates.

Could someone please help me on the way..

Thanks,

Feurich
 
Could you post the exact code you used to parse the string "12" as it definately doesn't parse when I trie it here.

Also DateTime.ParseExact may do what you need without requiring RegEx.
 
Typo :-(

Sorry something went wrong with my typing. The problem accours when the string is like "-12-2005" this is seen as a valid date.

[csharp]
string AccumValue = "-12-2005"
try
{
DateTime oDT = new DateTime();
oDT = DateTime.Parse(AccumValue);
textBox2.Text = AccumValue;
}
catch(FormatException)
{
textBox2.Text = "01-10-1900";
}
[/csharp]
 
So any ideas

Hi there,

So from my point of view -12-2005 is not a valid date string.
Or is -12-2005 a valid string date?

Feurich
 
If you have an exact format(s) you deem to be valid the DateTime.ParseExact is probably the easiest way.
Out of curiosity is the date being entered via a windows / web application? If so you may find using a control like the DatetimePicker for a windows app or a validation control for a web app would prevent incorrect input being entered.
 
OCR Application

The date is being read by an OCR engine and needs to be validated automaticaly. So there is no way of knowing what format will be read.
If the scanned image is very dirty (speckles or noise) the OCR engine can read anything. So all the data needs to be checked. There are numeric flieds and VarChar and date fields. But the date fields are the easiest
.
Ok, thanks for the help.

Feurich
 
Back
Top