我收到字符串未被识别为有效的日期时间。
以下函数中出现异常
public int GetTime()
{
string time = "17-07-2015 01:11:25"
time.Replace('-', '/');
DateTime oldDate = Convert.ToDateTime(time);
// Difference in days, hours, and minutes.
DateTime newDate =Convert.ToDateTime(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
TimeSpan ts = oldDate - newDate;
// Difference in days.
int differenceInhours = ts.Hours;
differenceInhours = differenceInhours * 60;
int differenceInMinutes = ts.Minutes + 1;
int BalanceTime = differenceInhours + differenceInMinutes;
return BalanceTime;
}
17-07-2015 01:11:25
此日期存储在数据库中
最佳答案
你可以尝试这个,因为你知道日期时间的格式:
DateTime oldDate = DateTime.ParseExact(time,"dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
关于c# - 异常 "String was not recognized as a valid DateTime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31447787/