从字符串 '10-April-2020' 转换为 DateTime
Convert to DateTime from a string '10-April-2020'
寻求帮助来转换我从 Web 表单收到的日期字符串,格式类似于“10-April-2020”。我需要将其以美国日期格式“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, "yyyy-MM-dd", culture, DateTimeStyles.None, out dateExpires))
{
// Do something
}
任何人都可以帮忙看看哪里出了问题吗?我也不允许更改 Ui/Form 来执行任何客户端日期操作,因此我的解决方案需要在 C# 代码隐藏文件中完成。
MM
表示月份数(从 01 到 12)
要解析 2020 年 4 月 10 日,您
需要 MMMM
,见
自定义日期和时间格式字符串
The "MMMM" custom format specifier represents the full name of the month
寻求帮助来转换我从 Web 表单收到的日期字符串,格式类似于“10-April-2020”。我需要将其以美国日期格式“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, "yyyy-MM-dd", culture, DateTimeStyles.None, out dateExpires))
{
// Do something
}
任何人都可以帮忙看看哪里出了问题吗?我也不允许更改 Ui/Form 来执行任何客户端日期操作,因此我的解决方案需要在 C# 代码隐藏文件中完成。
MM
表示月份数(从 01 到 12)
要解析 2020 年 4 月 10 日,您
需要 MMMM
,见
自定义日期和时间格式字符串
The "MMMM" custom format specifier represents the full name of the month