Instead of converting current date to string and then int
and doing the comparison, convert your parameter date string to DateTime
object and then compare like:
var parameterDate = DateTime.ParseExact("03/26/2015", "MM/dd/yyyy", CultureInfo.InvariantCulture);
var todaysDate = DateTime.Today;
if(parameterDate < todaysDate)
{
}
You can have your method as:
public static bool IsDateBeforeOrToday(string input)
{
DateTime pDate;
if(!DateTime.TryParseExact(input, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out pDate))
{
//Invalid date
//log , show error
return false;
}
return DateTime.Today <= pDate;
}
- Use
DateTime.TryParseExact
if you want to avoid exception in
parsing.
- Use
DateTime.Today
if you only want to compare date and ignore the
time part.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…