[英]ASP.NET “String was not recognized as a valid DateTime.”
First off, I realize there's a million pages discussing this already. 首先,我意识到已经有一百万页讨论过这个问题。 I have looked at least a hundred of them but cannot seem to make this work.
我已经看了至少一百个,但似乎无法使这项工作。 My date and time is presented as a string, compiled from javascript to grab client's local time.
我的日期和时间以字符串形式呈现,从javascript编译以获取客户端的本地时间。 It is formatted like this: 7/11/2015 8:34 PM.
它的格式如下:2015年7月11日下午8:34。 I currently have:
我目前有:
Dim datetimeformated = DateTime.ParseExact(lblDateTime.Text, "MM/dd/yyyy HH:mm tt", CultureInfo.InvariantCulture)
I have tried many different variants, but this should be correct I think, yet it does not work. 我尝试了很多不同的变体,但我认为这应该是正确的,但它不起作用。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 TIA
TIA
The correct format for your case is: M/dd/yyyy h:mm tt
, and perhaps even, M/d/yyyy h:mm tt
, if you can have the day of the month as a single digit. 您的案例的正确格式是:
M/dd/yyyy h:mm tt
,甚至可能是M/d/yyyy h:mm tt
,如果您可以将该月的某一天作为单个数字。
Explanation: Why your format string didn't work. 说明:为什么您的格式字符串不起作用。
MM
: means that you must always have 2 digits for the month, clearly not the case in your example. MM
:表示您必须始终拥有该月份的2位数,显然不是您示例中的情况。
dd
: again, means that you must always have 2 digits for the day of the month. dd
:再次表示您必须始终有2个数字作为该月的某一天。 Is that the case? 是这样的吗? Adjust the parameter if needed.
如果需要,调整参数。
HH
: This actually means that you are expecting the hour value as 2-digits using the 24-hour clock (00-23), which is clearly wrong on both accounts. HH
:这实际上意味着您希望使用24小时制(00-23)将小时值设为2位数,这在两个帐户中都显然是错误的。 You can have a single digit, and you are not using the 24-hour clock, because you are using the AM/PM
designator. 您可以使用单个数字, 而不使用24小时制,因为您使用的是
AM/PM
指示符。
Relevant documentation link: Custom Date and Time Format Strings . 相关文档链接: 自定义日期和时间格式字符串 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.