locked
Date Time format RRS feed

  • Question

  • Hi,

    Its a basic requirement where we have to send the datetime format as stated below , can we send the datetime format in below format , note variable should be datetype it should not be string.

    Format : MM/DD/YYYYT12:18PM

    Note "T" is there in between date and time.

    Thanks.

    Wednesday, June 8, 2016 6:48 AM

Answers

  • You are still contradicting yourself.

    A DateTime variable on its own does not have a format. It is just a DateTime - a piece of abstract data that represents a specific point in time. There is no "format".

    Formats only become important when you have to convert that DateTime into a string. In that case you want to indicate how you want that date and time represented as a piece of text.

    Now one reason you may need to convert a DateTime to a string is if you need to send that data to another piece of software such as a web service. But even in that case, the actual details of how the DateTime is "serialized" by your software to a common format and "deserialized" by the service at the other end is often hidden by the technology used (such as SOAP).

    So...you are sending your DateTime variable to a service? How do you communicate with that service? Is it a SOAP service (did you import a WSDL file into your project to set up the proxy objects)?

    • Marked as answer by XXX_K Monday, July 18, 2016 10:15 AM
    Wednesday, June 8, 2016 8:39 AM

All replies

  • You can convert a datetime into that format with:

    DateTime dt = DateTime.Now;
    
    String s = dt.ToString("MM/dd/yyyyThh:mmtt", System.Globalization.CultureInfo.InvariantCulture);

    Although your question kind of contradicts itself. You say you want the datetime to be in a given format, which implies you want to convert it to a string, but then you say "it should not be a string".

    Perhaps if you say what you mean by "send" the datetime format? Are you trying to serialize the datetime?

    Wednesday, June 8, 2016 7:55 AM
  • hi,

    Yes I wanted custome datetime format but that should be in DateTime type variable , that is the challenge. We have to send that to one of the service. Service is exepecting DateTime field with format as

    Format : MM/DD/YYYYT12:18PM

    Note "T" is there in between date and time.

    Also tried with TryParseExtract, no luck. Not sure something which can be done. Wanted to check the same if possible.

    Thanks.

    Wednesday, June 8, 2016 8:01 AM
  • You are still contradicting yourself.

    A DateTime variable on its own does not have a format. It is just a DateTime - a piece of abstract data that represents a specific point in time. There is no "format".

    Formats only become important when you have to convert that DateTime into a string. In that case you want to indicate how you want that date and time represented as a piece of text.

    Now one reason you may need to convert a DateTime to a string is if you need to send that data to another piece of software such as a web service. But even in that case, the actual details of how the DateTime is "serialized" by your software to a common format and "deserialized" by the service at the other end is often hidden by the technology used (such as SOAP).

    So...you are sending your DateTime variable to a service? How do you communicate with that service? Is it a SOAP service (did you import a WSDL file into your project to set up the proxy objects)?

    • Marked as answer by XXX_K Monday, July 18, 2016 10:15 AM
    Wednesday, June 8, 2016 8:39 AM
  • Yes , its a tibco service. We have been generating proxy out of it using svcutil.exe

    Thanks.

    Wednesday, June 8, 2016 9:27 AM
  • Well in that case, you should have a set of generated proxy classes that you use to communicate with the service.

    If any of those proxy classes require a DateTime then they will, hopefully, just have DateTime properties you can set. In which case, you don't worry about formats at all - you just set the properties from your DateTime variables.

    If, for some reason, the service is expecting the date and time as a String property in a documented format, then you can use the code I provided above to convert your DateTime into a String with that format.

    If none of these options make sense to you, then what problem are you having with the service? Are you trying to do something and getting an error?


    • Edited by RJP1973 Wednesday, June 8, 2016 9:36 AM
    • Proposed as answer by DotNet Wang Sunday, June 19, 2016 7:09 AM
    • Marked as answer by DotNet Wang Monday, June 20, 2016 9:06 AM
    • Unmarked as answer by XXX_K Monday, July 18, 2016 10:15 AM
    Wednesday, June 8, 2016 9:34 AM
  • Try first to understand what a DateTime is in .Net. 

    It is a value type (structure) which holds an Int64. It contains the ticks in 100th nanoseconds which have been starting at the imaginary Gregorian calendar date 01/01/01 at 0:0:0 (imaginary because the Gregorian calendar did not exist before 1200)

    The DateTime value type has many methods and properties (which are in fact also methods) to present the date in string formats or to count with. 

    One of the presentations is shown by RJP1973

    However, a datetime in .Net has never the format you are talking about, but it can be presented like that.


    Success
    Cor

    • Proposed as answer by DotNet Wang Sunday, June 19, 2016 7:09 AM
    • Marked as answer by DotNet Wang Monday, June 20, 2016 9:06 AM
    • Unmarked as answer by XXX_K Monday, July 18, 2016 10:15 AM
    Wednesday, June 8, 2016 9:49 AM
  • As far as in know the format of DateTime object would be same as the system datetime format. In order to pass in specific format you need to convert DateTime to string with specific format.

    String strDtTime=DateTime.Now.ToString("MM/dd/yyyyThh:mmtt", System.Globalization.CultureInfo.InvariantCulture);

     
    • Proposed as answer by DotNet Wang Sunday, June 19, 2016 7:09 AM
    • Marked as answer by DotNet Wang Monday, June 20, 2016 9:06 AM
    • Unmarked as answer by XXX_K Monday, July 18, 2016 10:15 AM
    Wednesday, June 8, 2016 10:32 AM