[英]error “String was not recognized as a valid DateTime.” in c#
I have created an android application in which I have used android date picker.我创建了一个 android 应用程序,其中我使用了 android 日期选择器。 While submitting form in my application I am getting date in this format "18-2-2021" and I want to format this date in "18-Feb-2021" format.
在我的申请中提交表格时,我得到了“18-2-2021”格式的日期,我想将该日期格式化为“2021 年 2 月 18 日”格式。 I don't want to force my user to update application so I want to format date in my c# web service code.
我不想强迫我的用户更新应用程序,所以我想在我的 c# web 服务代码中格式化日期。
I am trying to format date using below code but I am getting " String was not recognized as a valid DateTime. ".我正在尝试使用以下代码格式化日期,但我得到“字符串未被识别为有效的日期时间。 ”。
DateTime requestRaisedDate = DateTime.ParseExact(ApplicationID.REQUEST_RAISED_DATE, "dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture);
With 18-2-2021
you need to allow single-digit months: dd-M-yyyy
:对于
18-2-2021
,您需要允许个位数的月份: dd-M-yyyy
:
DateTime requestRaisedDate = DateTime.ParseExact(ApplicationID.REQUEST_RAISED_DATE, "dd-M-yyyy",System.Globalization.CultureInfo.InvariantCulture);
This allows 18-2-2021
and also 18-02-2021
.这允许
18-2-2021
和18-02-2021
。
See: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#month-m-format-specifier请参阅: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#month-m-format-specifier
DateTime requestRaisedDate = DateTime.ParseExact(ApplicationID.REQUEST_RAISED_DATE, "dd-M-yyyy",System.Globalization.CultureInfo.InvariantCulture);
Use Single M for single-digit month.使用 Single M 表示个位数的月份。 It will support 10, 11,12 by default.
默认支持10、11、12。 So, don't worry
所以,别担心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.