在 C# 中的日期格式之间转换时出错
Error converting between dateformats in c#
我 GridView
的日期字段中有以下数据:2016-01-24T00:00:00
在更新该字段之前,我想以 "dd/MM/yyyy" 格式存储该值。
为此,我正在执行以下操作:
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string formattedHolDate = DateTime.ParseExact(holDate, "dd/MM/yyyy", culture).ToString("MM/dd/yyyy");
但是,当运行代码时,我有以下错误:
String was not recognized as a valid DateTime.
正确的处理方式是什么?
我是如何阅读你的问题的。
2016-01-24T00:00:00 (what you have)
"dd/MM/yyyy" (what you want)
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
var myDate = DateTime.Parse(holDate);
string myFormattedDateString = myDate.ToString("dd/MM/yyyy");
你已经扭转了一些局面。
Parse
= 从字符串转换为日期(或其他对象,具体取决于实现)
ToString
- 从对象转换为字符串表示
编辑
由于 2016-01-24T00:00:00
实际上是 DateTime 的 ISO 8601 表示,因此您不需要 ParseExact
,只需 Parse
即可。
我 GridView
的日期字段中有以下数据:2016-01-24T00:00:00
在更新该字段之前,我想以 "dd/MM/yyyy" 格式存储该值。
为此,我正在执行以下操作:
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string formattedHolDate = DateTime.ParseExact(holDate, "dd/MM/yyyy", culture).ToString("MM/dd/yyyy");
但是,当运行代码时,我有以下错误:
String was not recognized as a valid DateTime.
正确的处理方式是什么?
我是如何阅读你的问题的。
2016-01-24T00:00:00 (what you have)
"dd/MM/yyyy" (what you want)
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
var myDate = DateTime.Parse(holDate);
string myFormattedDateString = myDate.ToString("dd/MM/yyyy");
你已经扭转了一些局面。
Parse
= 从字符串转换为日期(或其他对象,具体取决于实现)ToString
- 从对象转换为字符串表示
编辑
由于 2016-01-24T00:00:00
实际上是 DateTime 的 ISO 8601 表示,因此您不需要 ParseExact
,只需 Parse
即可。