简体   繁体   English

设置DateTime格式-如何将值保留为DateTime格式?

[英]Setting format of DateTime - how do I keep the value in DateTime format?

I have a DateTime being created in the format dd/MM/yyyy HH:mm:ss . 我有一个以dd/MM/yyyy HH:mm:ss格式创建的DateTime。 I am writing code that interacts with a third-party SOAP library that requires a DateTime variable, in the format yyyy-MM-dd HH:mm:ss . 我正在编写与需要DateTime变量的第三方SOAP库进行交互的代码,格式为yyyy-MM-dd HH:mm:ss

How do I change the way the information is stored in the DateTime variable, for the purpose of the call to the third-party SOAP library, ie no system-wide changes to dates? 为了调用第三方SOAP库,即如何在系统范围内更改日期,如何更改信息在DateTime变量中的存储方式?

I have investigated CultureInfo, which is mildly confusing and possibly too permanent a solution; 我已经研究了CultureInfo,它会使人感到困惑,并且解决方案可能太永久了。 the only time I need the DateTime changing is for an instance of this single call. 我唯一需要更改DateTime的时间是针对此单个调用的实例。

As an explanation, the library has a function GetOrders(DateTime startDate, DateTime endDate, TradingRoleCodeType roleType, OrderStatusCodeType statusType) . 作为说明,该库具有函数GetOrders(DateTime startDate, DateTime endDate, TradingRoleCodeType roleType, OrderStatusCodeType statusType) When attempting to perform the function with DateTimes as created, it generates an error "Sorry, the end date was missing, invalid, or before the start date. must be in YYYY-MM-DD or YYYY-MM-DD HH:MI:SS format, and after the start date.". 尝试使用创建的DateTimes执行功能时,它会生成错误消息“抱歉,结束日期丢失,无效或在开始日期之前。必须在YYYY-MM-DD或YYYY-MM-DD HH:MI中: SS格式,并在开始日期之后。”。 Given the format that is passed in as dd/MM/yyyy HH:mm:ss , I'd think this may be the problem. 给定以dd/MM/yyyy HH:mm:ss格式传递的格式,我认为这可能是问题所在。

I have a DateTime being created in the format dd/MM/yyyy HH:ii:ss 我有一个以dd / MM / yyyy HH:ii:ss格式创建的DateTime

No, you do not. 你不可以。 You have a DateTime. 您有一个DateTime。 It has no format. 没有格式。 It is a number - which is well documented, you know, in the documentation. 它是一个数字-您知道在文档中有充分的记录。 The string form is never used in a stored DateTime, only when generating the string for presentation. 仅在生成用于显示的字符串时,才在存储的DateTime中不使用字符串形式。

How do I change the way the information is stored in the DateTime variable, for the purpose of the call to the third-party SOAP library, ie no system-wide changes to dates? 为了调用第三方SOAP库,即如何在系统范围内更改日期,如何更改信息在DateTime变量中的存储方式?

You do not. 你不。 I would suggest you talk to your SOAP library - and it is not SOAP btw., IIRC the format you give as example is not valid in SOAP. 我建议您使用SOAP库-而且它不是SOAP btw。,作为示例,IIRC给出的格式在SOAP中无效。 Yes, bad news. 是的,坏消息。 Someone wants Pseudo-Soap. 有人想要伪肥皂。

http://www.w3schools.com/schema/schema_dtypes_date.asp http://www.w3schools.com/schema/schema_dtypes_date.asp

describes all valid date, time and datetime formats and yours is NOT there. 描述所有有效的日期,时间和日期时间格式,而您的格式不存在。

You can change the default format on a thread level back and forth, so one solution is to set it before calls into the soap library. 您可以在线程级别上来回更改默认格式,因此一种解决方案是在调用soap库之前对其进行设置。 Another one is to have someone fix the SOAP layer to accept standard formats. 另一个是让某人修复SOAP层以接受标准格式。

You can create a dummy date : 您可以创建一个虚拟日期:

public class SomeClass
{
    [XmlIgnore]
    public DateTime SomeDate { get; set; }

    [XmlElement("SomeDate")]
    public string SomeDateString
    {
        get { return this.SomeDate.ToString("yyyy-MM-dd HH:mm:ss"); }
        set { this.SomeDate = DateTime.Parse(value); }
    }
}

Source : Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss' --kbrimington 来源: 强制XmlSerializer将DateTime序列化为'YYYY-MM-DD hh:mm:ss'-- kbrimington

As it turns out, the problem - as some have pointed out - is not to do with the variable being a DateTime, nor its "format" which is not a "format", but is certainly the representation of the information in a method to be understood. 事实证明,问题(正如一些人指出的)与变量为DateTime无关,也不与变量“格式”(不是“格式”)有关,而是与信息在方法中的表示有关。被理解。

The basic issue with the information was a DateTime comparison between standard time and UTC time. 信息的基本问题是标准时间和UTC时间之间的DateTime比较。 The third-party library examined the DateTime as a UTC DateTime, which when at the right time of year to be caught with a difference in times can cause a problem comparing a DateTime; 第三方库将DateTime作为UTC DateTime进行了检查,如果在一年中的正确时间捕获时差,可能会导致比较DateTime时出现问题。 despite being presented as after the reference time to the user, the time is actually before the reference time when being calculated, meaning the comparison fails. 尽管在向用户显示参考时间之后显示了时间,但该时间实际上是在计算时参考时间之前显示的,这意味着比较失败。

The main takeaway for this question is to interrogate the information being passed to functions, if you don't have access to third-party library code nor access to documentation with sufficient detail, and errors are occurring when interacting with said third-party code. 如果您无权访问第三方库代码,也无权访问具有足够详细信息的文档,并且与该第三方代码进行交互时发生错误,则此问题的主要结论是询问传递给函数的信息。

Particularly, test various use cases to determine what variable values cause a failure and which cause successful execution of code; 特别是,测试各种用例以确定哪些变量值导致失败以及哪些变量代码成功执行; identify a pattern, and then test specific use cases that confirm the pattern. 确定一个模式,然后测试确认该模式的特定用例。 From there, determine the actual error that is occurring and code to fix the issue. 从那里,确定正在发生的实际错误并进行代码修复。

In the case of DateTimes, where the code understands DateTimeKinds such as C#, remember to test the different DateTimeKinds to establish whether they can be a part of the problem; 对于DateTimes,代码可以理解DateTimeKinds(例如C#),请记住测试不同的DateTimeKinds,以确定它们是否可能是问题的一部分。 its not happened to me often, but it has happened (as evidenced by this question). 它不是经常发生在我身上,但确实发生过(此问题已证明)。

Finally, error codes don't help much, and can lead to poor questions and poor advice; 最后,错误代码没有太大帮助,并且可能导致问题和建议不佳。 trial and error appears to be the best in cases similar to this. 在类似情况下,反复试验似乎是最好的。

You don't need to change how it's stored, as already mentioned above. 如上所述,您无需更改其存储方式。 You need to format is as a string according to ISO8601, which is what your SOAP service expects datetime parameter to be. 您需要根据ISO8601将其格式化为字符串,这是SOAP服务期望datetime参数所为。

Check How to parse and generate DateTime objects in ISO 8601 format and 检查如何解析和生成ISO 8601格式的DateTime对象,以及

Given a DateTime object, how do I get an ISO 8601 date in string format? 给定一个DateTime对象,如何获取字符串格式的ISO 8601日期?

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

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