【发布时间】:2020-04-15 03:43:21
【问题描述】:
我按照我的日期变量的字符串输出,按照格式解析,还是遇到了格式异常。
我可以知道我应该改变什么吗?
string DOB = retrieved.Entities[i].GetAttributeValue<AliasedValue>("Contact.birthdate").Value.ToString();
//output: 4/13/2018 12:00:00AM
DateTime DOB_formatted = DateTime.ParseExact(DOB, "MM/dd/yyyy", null);
//System.FormatException
解决方法:将对象转换为日期时间
DateTime DOB_formatted = Convert.ToDateTime(retrieved.Entities[i].GetAttributeValue<AliasedValue>("Contact.birthdate").Value);
【问题讨论】:
-
你能试试:
DateTime.ParseExact(DOB, "MM/dd/yyyy", new CultureInfo("en-US"))告诉我它是否有效吗? -
第一行的
.Value还不是日期时间吗?如果是object,你可能只需要投射它。 -
.Value之后,它是一个对象,如果我不将它转换为字符串,它会给我一个错误 -
@Tony 谢谢你的建议,同样的错误出现了。
-
DateTime DOB = (DateTime)retrieved.Entities[i].GetAttributeValue<AliasedValue>("Contact.birthdate").Value;