我使用以下方法来验证字符串是否为有效的日期时间:
public bool isDate(string date)
{
bool check = false;
try
{
DateTime converted_date = Convert.ToDateTime(date);
check = true;
}
catch (Exception)
{
check = false;
}
return check;
}
现在,每当我尝试传递这样的字符串时,都会捕获到异常“字符串未被识别为有效的日期时间”:
“ 2013年12月31日12:00:00”
我不明白为什么会这样。有人可以帮我解决这个问题吗?
最佳答案
您当前的区域性设置很可能与提供的格式日期不同。您可以尝试明确指定区域性:
CultureInfo culture = new CultureInfo("en-US"); // or whatever culture you want
Convert.ToDateTime(date, culture);