[英]String was not recognized as a valid DateTime in IIS server 7.5
I have looked at some of questions regarding "String was not recognized as a valid DateTime" but could not get answer. 我看过一些有关“字符串未被识别为有效的DateTime”但无法得到答案的问题。 Here is my issue.
这是我的问题。 I have code
我有代码
DateTime.ParseExact(dr[3].ToString(), "dd-MM-yyyy tt h:m:s", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat).ToString("MMM. dd yyyy hh:mm tt")
It works fine in localhost but having issues in IIS server. 它在localhost中可以正常工作,但在IIS服务器中有问题。 Eg.
例如。 date in my Sql Server 2008 R2 is "2014-03-05 09:10:17.040" Any help would be appreciated.
我的Sql Server 2008 R2中的日期为“ 2014-03-05 09:10:17.040”,我们将不胜感激。
Your custom format is wrong. 您的自定义格式错误。 If you use
DateTime.ParseExact
method, your format and string should match exactly. 如果使用
DateTime.ParseExact
方法,则格式和字符串应完全匹配。
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information.
使用指定的格式和特定于区域性的格式信息,将日期和时间的指定字符串表示形式转换为其等效的DateTime。 The format of the string representation must match the specified format exactly.
字符串表示形式的格式必须与指定的格式完全匹配。
Since your string starts with year, you need use yyyy
format first. 由于您的字符串以Year开头,因此您需要首先使用
yyyy
格式。 I assume your 03
is month and 05
is day. 我假设您的
03
是月份,而05
是一天。 h
format is for 1
to 12
. h
格式为1
到12
。 Since your hour is 09
, you should use HH
format which is for 00
to 23
. 由于您的小时数是
09
,因此您应该使用00
到23
HH
格式 。
m
format is ok for this string but is 0
to 59
. m
格式适用于此字符串,但可以为0
到59
。 You might need use mm
format which is for 00
to 59
. 您可能需要使用
00
到59
mm
格式 。 Same situation on your second part. 第二部分情况相同。
Also you need fff
format for your milliseconds part. 另外,您还需要
fff
格式的毫秒部分。
As a result, you should use yyyy-MM-dd HH:mm:ss.fff
format instead. 因此,您应该改用
yyyy-MM-dd HH:mm:ss.fff
格式。
For example; 例如;
string s = "2014-03-05 09:10:17.040";
var date = DateTime.ParseExact(s,
"yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be; 输出将是;
3/5/2014 9:10:17 AM
Here a demonstration
. 这里有
demonstration
。
After you parse your string, you can format is whatever you want with DateTime.ToString()
method like; 解析字符串后,您可以使用
DateTime.ToString()
方法设置所需的格式,例如;
date.ToString("MMM. dd yyyy hh:mm tt");
While debugging in asp.net i get format for dr as 17-03-2014 PM 2:53:04
在asp.net中调试时,我获得了格式为dr的17-03-2014 PM 2:53:04
But this doesn't have a same format with example date you write in your question! 但这与您在问题中写的示例日期格式不同! Anyway, for this case, you can use
dd-MM-yyyy tt h:mm:ss
format. 无论如何,对于这种情况,您可以使用
dd-MM-yyyy tt h:mm:ss
格式。
For example; 例如;
var date = DateTime.ParseExact("17-03-2014 PM 2:53:04",
"dd-MM-yyyy tt h:mm:ss",
CultureInfo.InvariantCulture);
Console.WriteLine(date);
Unfortunately, looks like your dates don't have a standart date format each others in your database. 不幸的是,看来您的日期在数据库中彼此之间没有标准日期格式。 In this such a case, it is impossible to parsing them all in a one format.
在这种情况下,不可能将它们全部解析为一种格式。
Only you can use DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
overload which you can use more than one format in a string array for your strings. 只有您可以使用
DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
重载 ,您可以在字符串数组中为字符串使用多种格式。
You're using ParseExact which will ONLY work if the format of what you're parsing matches the format of the string. 您正在使用ParseExact,它仅在您解析的格式与字符串的格式匹配时才起作用。
In your code you're expecting "dd-MM-yyyy tt h:m:s"
在您的代码中,您期望
"dd-MM-yyyy tt h:m:s"
In the example you gave ("2014-03-05 09:10:17.040"), it's in 在您提供的示例中(“ 2014-03-05 09:10:17.040”),它位于
yyyy-MM-dd HH:mm:ss
Try changing it to DateTime.Parse()
and let it determine the format itself. 尝试将其更改为
DateTime.Parse()
并让其确定格式本身。
Try This: 尝试这个:
DateTime.ParseExact(dr[3].ToString(), "yyyy-MM-dd hh:mm:ss.fff",
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat).
ToString("MMM.dd yyyy hh:mm tt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.