I’m trying to convert a string to datetime to validate if user input is actually a date.
The error I’m getting is:
Cannot implicitly convert type bool to System.DateTime.
I’ve been looking online for a while and can’t find anything specific enough to help me understand.
Code:
public bool is21YearsOfAge(string argument) { DateTime _parsedDateArgument; DateTime convertStringToDate = System.DateTime.TryParse(argument, out >_parsedDateArgument); if (convertStringToDate > DateTime.Now) { //do something } }
Thanks in advance.
The
TryParsemethod returns aboolthat informs you whether the parse was successful, rather than throwing an exception like theParsemethod does. Try doing this:If
argumentis a date,convertStringToDatewill contain that date as aDateTime.