解析 DateTime 的两种不同的字符串格式
Two different string formats for parsing DateTime
我必须解析两种不同类型的 DateTime 格式:dd/MM/yyy
和 dd-MM-yy
。
该字符串来自 item.EmploymentDate
,它通常是两种格式之一,我需要将其解析为 DateTime
个对象。
到目前为止我的代码:
var formatStrings = new string[] { "dd/MM/yyyy", "dd-MM-yyy" };
DateTime.TryParseExact(item.EmploymentDate, formatStrings, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime resultDate);
employee.Details.EmploymentDate = resultDate;
但是,resultDate
最终成为默认的 DateTime 对象 {1/1/0001 12:00:00 AM}
,而不是 item.EmploymentDate
中的值。我可以确认它们不为空。第一个是"30-03-1999"
。
我做错了什么?
"dd-MM-yyy"
中有三个yyy
而不是yyyy
,但是30-03-1999有4位数字代表年份
我必须解析两种不同类型的 DateTime 格式:dd/MM/yyy
和 dd-MM-yy
。
该字符串来自 item.EmploymentDate
,它通常是两种格式之一,我需要将其解析为 DateTime
个对象。
到目前为止我的代码:
var formatStrings = new string[] { "dd/MM/yyyy", "dd-MM-yyy" };
DateTime.TryParseExact(item.EmploymentDate, formatStrings, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime resultDate);
employee.Details.EmploymentDate = resultDate;
但是,resultDate
最终成为默认的 DateTime 对象 {1/1/0001 12:00:00 AM}
,而不是 item.EmploymentDate
中的值。我可以确认它们不为空。第一个是"30-03-1999"
。
我做错了什么?
"dd-MM-yyy"
中有三个yyy
而不是yyyy
,但是30-03-1999有4位数字代表年份