问题描述
在转换我从网络表单中收到的日期字符串时需要帮助,该表单的格式类似于“ 2020年4月10日”。我需要以美国日期格式“ yyyy-mm-dd”将其保存到数据库中,以便提供的示例日期为“ 2020-04-10”。
这是我到目前为止的内容,抱怨它不是有效的日期时间。
string LicenseExpiry = LicenseExpiry.Text;
IFormatProvider culture = new CultureInfo("en-US",true);
DateTime dateExpires = DateTime.ParseExact(LicenseExpiry,"yyyy-MM-dd",culture);
我也尝试了以下同样失败的事情。
DateTime dateExpires;
string LicenseExpiry = LicenseExpiry.Text;
IFormatProvider culture = new CultureInfo("en-US",true);
if (DateTime.TryParseExact(LicenseExpiry,culture,DateTimeStyles.None,out dateExpires))
{
// Do something
}
任何人都可以帮忙找出问题出在哪里吗?我也不允许更改Ui / Form来执行任何客户端日期操作,因此我的解决方案需要在文件后面的C#代码中完成。
解决方法
MM
表示月份号(从01到12)
要解析2020年4月10日,您
需要MMMM
,请参阅
Custom date and time format strings
“ MMMM”自定义格式说明符表示月份的全名