Specifying a Format
Consider using the DateTime.TryParseExact()
method which will allow you to explicitly define the format your string is in :
// This will attempt to parse your date in exactly the format provided
if(!DateTime.TryParseExact(textBoxDatumVanStorting.Text,"yyyy/MM/dd", null, DateTimeStyles.None, out Date2))
{
// Your date is not valid, consider doing something
}
A Matter of Culture
In the above example, the third parameter being passed in represents the specific culture / format that you expect to use to parse the date. Using null
will default to the current culture, but if you need to explicitly specify this, you can do so here as seen using the invariant culture :
Date output;
if(!DateTime.TryParseExact(input,"yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out output))
{
// Uh oh again...
}