简体   繁体   English

无法将字符串识别为有效的DateTime。 “28 \\ 08 \\ 2014”

[英]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.

相关问题 字符串日期未被识别为有效的日期时间。 - String date was not recognized as a valid DateTime.' 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 无法将字符串识别为有效的DateTime。 引发异常 - String was not recognized as a valid DateTime. Throws an Exception 错误:字符串未被识别为有效的DateTime。 - Error: String was not recognized as a valid DateTime. 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 字符串未被识别为有效的日期时间。 将字符串转换为日期时间 - String was not recognized as a valid DateTime. Converting string to DateTime DateTime.ParseExact赋予String未被识别为有效的DateTime。 - DateTime.ParseExact gives String was not recognized as a valid DateTime. 迄今为止的字符串解析错误“字符串未被识别为有效的DateTime。” - String to date parsing error “String was not recognized as a valid DateTime.” 无法将字符串识别为有效的DateTime。 2015年6月26日 - String was not recognized as a valid DateTime. 6-26-2015 “字符串未被识别为有效的DateTime。”Window 7计算机环境中出现错误 - “String was not recognized as a valid DateTime.” Error occur in Window 7 computer environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM