-1

I have a DateTime being created in the format dd/MM/yyyy HH:mm:ss. 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.

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, i.e. no system-wide changes to dates?

I have investigated CultureInfo, which is mildly confusing and possibly too permanent a solution; the only time I need the DateTime changing is for an instance of this single call.

As an explanation, the library has a function 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.". Given the format that is passed in as dd/MM/yyyy HH:mm:ss, I'd think this may be the problem.

David Smithson
  • 464
  • 2
  • 13
  • 33
  • 1
    What is the `ii` format specifier? Do you mean `mm`? – Tim Schmelter Sep 08 '14 at 12:40
  • Also, what means _"change the way the information is stored in the DateTime"_, do you want to store a string instead if you want to retain the format for the 3rd-party library? It's way too abstract. – Tim Schmelter Sep 08 '14 at 12:41
  • 1
    DateTime has no format. The representation of a date through a string requires a `format`. See [Standard Date and Time Format Strings](http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx) and [Custom Date and Time Format Strings](http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) – Steve Sep 08 '14 at 12:42
  • 2
    `change the way the information is stored in the DateTime variable`. The data isn't stored in a particular string format like you're thinking. You just want to use `ToString()` and specify the format you want – Jonesopolis Sep 08 '14 at 12:42
  • CultureInfo is used whether you like it or not. But like others have said, just use the desired format specifier when communicating with the third-party library. I'm suspecting the library needs a string, not a "DateTime variable". – bzlm Sep 08 '14 at 12:44
  • are you storing Date in string variable? you can convert date by date.Tostring("yyyy-MM-dd HH:mm:ss); – V2Solutions - MS Team Sep 08 '14 at 12:45
  • I've edited my question to try and better reflect my problem. – David Smithson Sep 08 '14 at 13:07
  • @David Smithson: I'll ask you too: Do you have an original date as a `String` or as `DateTime`? – Paul Sep 08 '14 at 13:18
  • Then go back to whoever gave you the API and tell them to fix it. NO (!) standard compliant SOAP library will generate this date time format because it is not legal by SOAP standards. – TomTom Sep 08 '14 at 13:20

4 Answers4

2

I have a DateTime being created in the format dd/MM/yyyy HH:ii:ss

No, you do not. You have a 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.

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, i.e. no system-wide changes to dates?

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. Yes, bad news. Someone wants Pseudo-Soap.

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. Another one is to have someone fix the SOAP layer to accept standard formats.

Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • My apologies for not knowing the exact parlance for the third-party code I'm working with. All I can look at when constructing my question is the error message saying I'm passing a DateTime in with the wrong format. – David Smithson Sep 08 '14 at 13:05
  • A DateTime object? Or some string? As discussed above, there is no format associated with an instance of System.DateTime. You can output in a separate format with .ToString() . – Ray Booysen Sep 08 '14 at 13:12
  • @DavidSmithson Please do not blame abuse of basic .NET and programming concepts on "the exact parlance for the third party code". That numeric / calendar objects are not having a format is baseline knowledge. You should basically not look at the error but also have a solid understanding of core concepts. Nothing in your question in regards to "how do i change the format of the data in a DateTime object" has anything to do with third party settings. – TomTom Sep 08 '14 at 13:18
0

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

Community
  • 1
  • 1
Margus
  • 19,694
  • 14
  • 55
  • 103
0

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.

The basic issue with the information was a DateTime comparison between standard time and UTC time. 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; 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; 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.

David Smithson
  • 464
  • 2
  • 13
  • 33
0

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.

Check How to parse and generate DateTime objects in ISO 8601 format and

Given a DateTime object, how do I get an ISO 8601 date in string format?