简体   繁体   English

调整时区-将XML DateTime转换为SQL DateTime

[英]Adjusting Timezone - Convert XML DateTime to SQL DateTime

We are using TypedDataSet in our application. 我们在应用程序中使用TypedDataSet。 Data is passed to procedure in form of XML for insert/update. 数据以XML的形式传递给过程以进行插入/更新。

Now after populating DE with data, datetime remains the same though timezone information is added as below. 现在,在用数据填充DE之后,尽管添加了以下时区信息,但datetime仍然相同。

Date in DB: 2009-10-29 18:52:53.43 Date in XML: 2009-10-29T18:52:53.43-05:00 DB中的日期:2009-10-29 18:52:53.43 XML中的日期:2009-10-29T18:52:53.43-05:00

Now when I am trying to convert below XML to SQL DateTime it is adjusting 5 hours and I am getting 现在,当我尝试将以下XML转换为SQL DateTime时,它正在调整5个小时,

2009-10-29 23:52:53.430 as the final output, which is wrong. 2009-10-29 23:52:53.430作为最终输出,这是错误的。 Need to find a way to extract datetime from below XML snippet ignoring timezone. 需要找到一种从下面的XML代码片段中提取datetime的方法,而忽略时区。

I have XML in following format, with timezone difference -05.00 我有以下格式的XML,时区不同-05.00

<Order>
   <EnteredDateTime>2009-10-29T18:52:53.43-05:00</EnteredDateTime>
</Order>

First convert into typed xsd date time and then use the value method to convert that value to a sql datetime. 首先将其转换为键入的xsd日期时间,然后使用value方法将该值转换为sql datetime。

select convert(xml, '<DateTimeWithTimeZone>2012-09-15T16:08:14.787-05:00</DateTimeWithTimeZone>').value('xs:dateTime(/DateTimeWithTimeZone[1])', 'datetime')

Convert datetime with time zone to SQL datetime 将带时区的datetime转换为SQL datetime

2009-10-29 18:52:53.43 is not the same time as 2009-10-29T18:52:53.43-05:00. 2009-10-29 18:52:53.43与2009-10-29T18:52:53.43-05:00不是同一时间。 It's the first step that is incorrect. 这是不正确的第一步。 The conversion from XML to SQL DateTime is correct. 从XML到SQL DateTime的转换是正确的。

ISO 8601 ISO 8601

sorry for simply forwarding with an external link, but i found this resource useful recently and always favour authoritative info. 很抱歉仅通过外部链接进行转发,但我最近发现此资源很有用,并且始终偏爱权威信息。

Coding Best Practices Using DateTime in the .NET Framework 在.NET Framework中使用DateTime编码最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM