gpt4 book ai didi

c# - 日期时间格式问题

转载 作者:行者123 更新时间:2023-11-30 20:58:20 24 4
gpt4 key购买 nike

我在将日期的字符串表示形式转换为日期时间对象时遇到困难。我的字符串是 "2013424",如果我使用以下代码将字符串转换为日期时间,则它会失败..

string format = "yyyyMd";
DateTime.TryParseExact("2013424", format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtAnswerDate);

dtAnswerDate does not contain converted representation of string value.

但是如果我的字符串的值类似于 "20130424" 那么它就可以正常工作。请问有什么建议吗?

该值来自 Microsoft HealthVault,在我的代码中使用 XPath 表达式。此 XPath 表达式由最终用户提供。 XPath 表达式选择一个节点并使用 InnerText 属性返回值。我正在编写以下代码以供引用。

string strItemXML = healthRecordItem.GetItemXml();
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(strItemXML);
XmlNode xpathNode = xDoc.SelectSingleNode(xpath);
When = healthRecordItem.LastUpdated != null ? healthRecordItem.LastUpdated.Timestamp : healthRecordItem.EffectiveDate;
if (xpathNode != null)
{
return xpathNode.InnerText;
}

'strItemXML' 包含以下 XML

`<thing>  
<thing-id version-stamp="a40f8c41-0329-4b0f-af56-33040a1f3ecf">de631835-c7b6-43dd-9ae9-528c26ab2204</thing-id>
<type-id name="Appointment">4b18aeb6-5f01-444c-8c70-dbf13a2f510b</type-id>
<thing-state>Active</thing-state>
<flags>0</flags>
<eff-date>2013-04-23T14:33:00Z</eff-date>
<created>
<timestamp>2013-02-15T12:48:42.74Z</timestamp>
<app-id name="InstantPHR Personal Health Record">afc8707e-c160-4b7f-9856-69d5c790b1a9</app-id>
<person-id name="Mohammed Owes">f848a184-878d-4cef-999f-dff54d2e5db2</person-id>
<access-avenue>Online</access-avenue>
<audit-action>Created</audit-action>
<master-app-id>8df99894-5f36-489a-a8fd-ffe6b25b8efb</master-app-id>
</created>
<updated>
<timestamp>2013-04-24T07:44:58.38Z</timestamp>
<app-id name="InstantPHR Personal Health Record">afc8707e-c160-4b7f-9856-69d5c790b1a9</app-id>
<person-id name="Mohammed Owes">f848a184-878d-4cef-999f-dff54d2e5db2</person-id>
<access-avenue>Online</access-avenue>
<audit-action>Updated</audit-action>
<master-app-id>8df99894-5f36-489a-a8fd-ffe6b25b8efb</master-app-id>
</updated>
<data-xml>
<appointment>
<when>
<date>
<y>2013</y>
<m>4</m>
<d>24</d>
</date>
<time>
<h>14</h>
<m>33</m>
<s>0</s>
</time>
</when>
<service>
<text>RREEESSSTTTT</text>
</service>
<clinic>
<name>
<full>Mohammed Owes</full>
</name>
</clinic>
</appointment>
<common>
<source>InstantPHR</source>
<extension source="phr.getrealconsulting.com.appointment_v2"/>
</common>
</data-xml>
</thing>`

'xpath' 值为“/thing/data-xml/appointment/when/date”。所以我的代码将返回“2013424”

最佳答案

更新

看起来您在该 xml 中分隔了值。

您可以通过分别提取年、月和日并将其输入采用这三个参数的 DateTime 构造函数来完全避免解析:

var date = new DateTime(year, month, day);

不幸的是,我对 XPath 不太感兴趣,所以您必须自己修改那部分。


我认为这里的问题是月份或日期中的数字可能存在歧义。我知道你举的日期其实并没有歧义,但还是失败了,因为潜力还在。

例如:2013111。这是 11 月 1 日还是 1 月 11 日?这就是前导 0 起作用的原因,因为它可以分隔数字以消除歧义。

我认为除非添加空格或指定前导零,否则无法使这种格式起作用。

空白示例:

string format = "yyyy M d";
DateTime date = DateTime.ParseExact("2013 4 24", format, CultureInfo.InvariantCulture);

您没有说明任何限制或“2013424”值的来源,因此我无法轻易提出解决此特定于您的代码的问题的方法。

当然,如果您不从输入的角度捕捉无歧义格式的值,您也会遇到这些歧义问题,试图理解什么是一天,什么是是一个月。

供引用,you can find the documentation for DateTime format strings here .

关于c# - 日期时间格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16189332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com