locked
DateTime Format - non-standard format RRS feed

  • Question

  • Hi,

    I'm writing a test tool in C#/VS2005/.NET 2.0, which is a client consuming a Web Service written in Java and utilizing an Oracle backend, running in WebSphere. For some reason, the expected format for date time (specified in the WSDL) is really strange:

    2008-04-03T09:42:12.000

    I'm unable to find any guidance on how to formate my datetime objects such that, when I submit my request, the web service can parse and accept them. I've looked at DateTimeFormatInfo but still haven't found any formats which apply.

    My current format is the standard: {8/20/2006 12:00:00 AM}

    How can I programatically massage the format into what's expected by the service? NOTE: the method requires this value be inputted as a DateTime, and will not accept a string.

    Thanks in advance...

    John O.
    http://thoughtsonqa.blogspot.com
    Thursday, April 3, 2008 3:57 PM

Answers

  • Try doing something like this at the beginning of the application:

     

    Code Snippet

    CultureInfo culture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();

    DateTimeFormatInfo dateTimeFormat = culture.DateTimeFormat;

    dateTimeFormat.ShortDatePattern = "";

    dateTimeFormat.LongTimePattern = "yyyy-MM-ddTHH:mm:ss.fff";

    Thread.CurrentThread.CurrentCulture = culture;

     

     

    Thursday, April 3, 2008 7:48 PM

All replies

  •  

    Code Snippet

    date.ToString("s");

     

    or maybe:

     

    Code Snippet
    date.ToString("o");

     

    One of those should get you started.

     

    http://msdn2.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
    Thursday, April 3, 2008 4:08 PM
  • Yeah, I was hoping it would be that easy. I tried that. However, as I mentioned in my post - the web service is expecting this to be submitted as a DateTime object and not a string.

    That would have been the quick way out!
    Thursday, April 3, 2008 4:10 PM
  •  

    If the webservice is looking for a DateTime, try:

     

    Code Snippet
    DateTime.Parse("2008-04-03T09:42:12.000"); 

     

     

    That worked just fine for me.

    Thursday, April 3, 2008 4:14 PM
  • So what's odd is that, yes, I'm passing a DateTime and one would expect that the format of the DateTime doesn't matter (it should just be 1's and 0's, right?). Unfortunately, somehow I have to modify that DateTime object (not a string representation) such that it returns yyyy-MM-ddTHH:mmTongue Tieds

    I admit to being befuddled. Why/how a datetime can be formatted itself is beyond me; HOW to format it in C# to be other than the standard .NET format is way beyond me.

    Any help = much appreciated.
    Thursday, April 3, 2008 4:52 PM
  • You can use
    DateTime.ParseExact Method (String, String, IFormatProvider) to parse datetime string with custom format
    Thursday, April 3, 2008 5:03 PM
  • Yes, correct - if I wanted to read a string in a certain format into the .NET DateTime object, I could use Parse or ParseExact. Unfortunately, I'm not trying to do that... I already have a DateTime object (I'm creating a random date for test purposes). My problem is 1) the Web service expects a datetime object submitted (not a string) and 2) the default .NET datetime object format is different, somehow, from the Web service's format.

    So the Web service is looking for a DateTime object (not a string) formatted as yyyy-DD-mmTHH:mmTongue Tieds
    .NET's DateTime object format is d/mm/yyyy m:hhTongue Tieds

    How do I get my .NET object format to 'sync up' with that of the Web service (short of 'dumbing down' the web service to accept string for the DateTime object)?

    Thanks again. I do appreciate everyone's efforts. Believe me, I've been hitting my head on a wall around this all day. Unfortunately it's not as easy as string manipulation.

    John O.
    Thursday, April 3, 2008 5:28 PM
  • So create your own class/structure that will do the work for you.

    Look at the example of a custom DateTime object:

    http://www.koders.com/csharp/fidFC52C3176901B39FB1F76070627F8C14CF0BB9F4.aspx

     

    Thursday, April 3, 2008 5:49 PM
  • Try doing something like this at the beginning of the application:

     

    Code Snippet

    CultureInfo culture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();

    DateTimeFormatInfo dateTimeFormat = culture.DateTimeFormat;

    dateTimeFormat.ShortDatePattern = "";

    dateTimeFormat.LongTimePattern = "yyyy-MM-ddTHH:mm:ss.fff";

    Thread.CurrentThread.CurrentCulture = culture;

     

     

    Thursday, April 3, 2008 7:48 PM
  • David, that did it! Thank you very much - I never would have thought to set the format for the current thread. I'm up, running, and generating tons of (valid) data.

    Thanks,

    John O.
    http://thoughtsonqa.blogspot.com
    Friday, April 4, 2008 3:12 AM