• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

Date Validation - Is this a real date?

I need to validate a date provided by a user.  I'm currently using the attached snippet, however, it returns a true when validating 2/31/1999.  There has to be a better way to do this.
private bool IsDate(string dt)
	DateTime testDate;
	if (!DateTime.TryParse(dt, out testDate))
		return true;
	return false;

Open in new window

1 Solution
Try the following method


string dateString, format;  
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
// Parse date-only value with invariant culture.
dateString = "06/15/2008";
format = "MM/dd/yyyy";
try {
   result = DateTime.ParseExact(dateString, format, provider);

Open in new window

Tony McCreathTechnical SEO ConsultantCommented:
Its accepting US formatted dates "MM/dd/yyyy".

Your probably wanting international dates "dd/MM/yyyy"

Changing Aleksei_Malkov suggestion to use  "dd/MM/yyyy" will probably get you what you want.

p.s. it indicates that your current culture may be set to US

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now