[英]String was not recognized as a valid DateTime. “28\08\2014”
I have this format in our database "26\\08\\2014" but when I try to use the Convert.ToDateTime and DateTime.Parse its not working but when I change the data from "26\\08\\2014" to "08\\26\\2014" using the Immediate Window, it works. 我的数据库“ 26 \\ 08 \\ 2014”中有这种格式,但是当我尝试使用Convert.ToDateTime和DateTime.Parse时,它无法正常工作,但是当我将数据从“ 26 \\ 08 \\ 2014”更改为“ 08 \\ 26”时, \\ 2014”(使用即时窗口),即可正常运行。
Current Code: 当前代码:
string dateFromDB = @"26/08/2014"; //day/month/year DateTime date = DateTime.Parse(dateFromDB);
You need to escape the backslashes in the format string to prevent them from being interpreted by the format engine: 您需要转义格式字符串中的反斜杠,以防止格式引擎解释它们:
@"MM\\dd\\yyyy"
Also, 28 is not a valid month. 同样,28不是有效月份。
Try this one: 试试这个:
DateTime.ParseExact(@"28\08\2014", @"dd\\MM\\yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None)
尝试:
DateTime.ParseExact("28\\08\\2014", @"dd\\MM\\yyyy", null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.