DateTime过期检查

时间:2013-10-26 18:42:01

标签: c# datetime

我正在尝试构建一个扩展程序,用于检查特定日期是否已过期(与今天的日期相比)。

示例:DateTime.Now是:10/26/2013 11:34:59 AM

如果特定日期时间为:10/22/2013 11:34:59 AM,则返回true(已过期)

如果特定日期时间为10/28/2013 11:34:59 AM,则返回false(未过期)

这是一种正确的方法吗? (比较时间也因为我不需要日期)

public static bool IsExpired(this string specificDate)
{
   bool flag = false;
   DateTime currentDate = DateTime.Now;

   DateTime target;

   if (DateTime.tryParse(specificDate, out target)
   {
     flag = target < currentDate;          
   }

   return flag;     
}

1 个答案:

答案 0 :(得分:9)

试试这个:

public static bool IsExpired(this DateTime specificDate)
{
   return specificDate < DateTime.Now;  
}