I have this in some WSDL:
我在一些WSDL中有这个:
<element name="startDate" type="xsd:dateTime"/>
<element name="endDate" type="xsd:dateTime"/>
Which results in the following text in the SOAP envelope:
这导致SOAP信封中的以下文本:
<startDate>2008-10-29T12:01:05</endDate>
<endDate>2008-10-29T12:38:59.65625-04:00</endDate>
Only some times have the milliseconds and zone offset. This causes me a headache because I'm trying to get a range of 37 minutes and 54 seconds in this example, but because of the offset I end up with 4 hours, 37 minutes, 54.65625 seconds. Is this some kind of rounding error in DateTime? How do I prevent this from happening?
只有几次具有毫秒和区域偏移。这让我头疼,因为我试图在这个例子中获得37分54秒的范围,但由于偏移我最终得到了4小时37分54.65625秒。这是DateTime中的某种舍入错误吗?我该如何防止这种情况发生?
2 个解决方案
#1
4
I suspect your endDate value has the Kind property set to DateTimeKind.Local.
我怀疑你的endDate值将Kind属性设置为DateTimeKind.Local。
You can change this to DateTimeKind.Unspecified as follows:
您可以将此更改为DateTimeKind.Unspecified,如下所示:
endDate = DateTime.SpecifyKind(endDate, DateTimeKind.Unspecified)
after which I believe it will be serialized without the timezone offset.
之后我相信它会在没有时区偏移的情况下被序列化。
Note that you will get a DateTime with DateTimeKind.Local if you have initialized it using DateTime.Now or DateTime.Today, and DateTimeKind.Utc if you have initialized it using Datetime.UtcNow.
请注意,如果使用DateTime.Now或DateTime.Today初始化它,则将获得DateTimeKind.Local的DateTime;如果使用Datetime.UtcNow初始化了DateTimeKind.Utc,则将获得DateTimeKind.Utc。
#2
1
What are you using to generate the date? If you are building this XML in your code rather than using some serializer (WCF or XmlSerializer) you could use System.Xml.XmlConvert to generate and interpret the date as follows:
你用什么来生成日期?如果要在代码中构建此XML而不是使用某些序列化程序(WCF或XmlSerializer),则可以使用System.Xml.XmlConvert生成和解释日期,如下所示:
To create the string to put in the XML:
要创建要放入XML的字符串:
DateTime startDate = DateTime.Now;
string startDateString = System.Xml.XmlConvert.ToString(startDate);
To get the date out of the XML:
要从XML中获取日期:
DateTime startDateFromXml = System.Xml.XmlConvert.ToDateTime(startDateString);
If you start with two DateTime instances that differ by 37 minutes and 54 seconds before you push them into XML they will still differ by 37 minutes and 54 seconds after you pull them out of the XML.
如果从两个DateTime实例开始,这些实例在将它们推入XML之前相差37分54秒,那么在将它们从XML中拉出后,它们仍然会有37分54秒的差异。
#1
4
I suspect your endDate value has the Kind property set to DateTimeKind.Local.
我怀疑你的endDate值将Kind属性设置为DateTimeKind.Local。
You can change this to DateTimeKind.Unspecified as follows:
您可以将此更改为DateTimeKind.Unspecified,如下所示:
endDate = DateTime.SpecifyKind(endDate, DateTimeKind.Unspecified)
after which I believe it will be serialized without the timezone offset.
之后我相信它会在没有时区偏移的情况下被序列化。
Note that you will get a DateTime with DateTimeKind.Local if you have initialized it using DateTime.Now or DateTime.Today, and DateTimeKind.Utc if you have initialized it using Datetime.UtcNow.
请注意,如果使用DateTime.Now或DateTime.Today初始化它,则将获得DateTimeKind.Local的DateTime;如果使用Datetime.UtcNow初始化了DateTimeKind.Utc,则将获得DateTimeKind.Utc。
#2
1
What are you using to generate the date? If you are building this XML in your code rather than using some serializer (WCF or XmlSerializer) you could use System.Xml.XmlConvert to generate and interpret the date as follows:
你用什么来生成日期?如果要在代码中构建此XML而不是使用某些序列化程序(WCF或XmlSerializer),则可以使用System.Xml.XmlConvert生成和解释日期,如下所示:
To create the string to put in the XML:
要创建要放入XML的字符串:
DateTime startDate = DateTime.Now;
string startDateString = System.Xml.XmlConvert.ToString(startDate);
To get the date out of the XML:
要从XML中获取日期:
DateTime startDateFromXml = System.Xml.XmlConvert.ToDateTime(startDateString);
If you start with two DateTime instances that differ by 37 minutes and 54 seconds before you push them into XML they will still differ by 37 minutes and 54 seconds after you pull them out of the XML.
如果从两个DateTime实例开始,这些实例在将它们推入XML之前相差37分54秒,那么在将它们从XML中拉出后,它们仍然会有37分54秒的差异。