none
DateTime C# - (YYYY-MM-DDThh: mm: ss) as 24hour

    Question

  • Hi there,

    I need send a date formated like this(YYYY-MM-DDThh: mm: ss) and the hour as a 24-hour (military).

    I'm using C# this way :

    DateTime

     

    startTime = DateTime.Now;

     

    and I take the actual time but it is like (dd/MM/yyyy HH:mm:ss).

    I cannot just rearrange the things because it was a string and I must send a "datetime", not a string.

    Can someone help?

    Thanks

    Sidney

    startTime = DateTime.Now;
    Tuesday, August 31, 2010 1:18 PM

Answers

  • You can format the date as string in the format that you like:

    so you can use

    startTime.ToString("YYYY-MM-DD HH:mm:ss")
    but the return is always string and cannot be date - because the DateTime is an object and contain all the needed information (day, month, year, hour) and the ToString is how to represent this date in what format - so always when you are formatting you are converting to String.


    Ali Hamdar (alihamdar.com)
    • Proposed as answer by Googgler Tuesday, August 31, 2010 9:30 PM
    • Marked as answer by Alan_chenModerator Thursday, September 02, 2010 7:35 AM
    Tuesday, August 31, 2010 1:28 PM
  • Thanks ALI,

    I'll try doing that, and hope the WebService accepts this way (a string) because the documentation asks for a DateTime type.

    I've corrected it,

    the capitalized doesn't work at Year and Day positions.

    it worked this way:

    startTime.ToString("yyyy-MM-dd HH:mm:ss")

    ando so on!

    Regards,

    Sidney

    Tuesday, August 31, 2010 1:33 PM

All replies

  • You can format the date as string in the format that you like:

    so you can use

    startTime.ToString("YYYY-MM-DD HH:mm:ss")
    but the return is always string and cannot be date - because the DateTime is an object and contain all the needed information (day, month, year, hour) and the ToString is how to represent this date in what format - so always when you are formatting you are converting to String.


    Ali Hamdar (alihamdar.com)
    • Proposed as answer by Googgler Tuesday, August 31, 2010 9:30 PM
    • Marked as answer by Alan_chenModerator Thursday, September 02, 2010 7:35 AM
    Tuesday, August 31, 2010 1:28 PM
  • Thanks ALI,

    I'll try doing that, and hope the WebService accepts this way (a string) because the documentation asks for a DateTime type.

    I've corrected it,

    the capitalized doesn't work at Year and Day positions.

    it worked this way:

    startTime.ToString("yyyy-MM-dd HH:mm:ss")

    ando so on!

    Regards,

    Sidney

    Tuesday, August 31, 2010 1:33 PM
  • True, sorry for the mistake and you need to leave HH in capital to get 24 hours as you did!
    Ali Hamdar (alihamdar.com)
    Tuesday, August 31, 2010 1:43 PM
  • If you need to send a DateTime value, you don't need to care about a display format.
    Tuesday, August 31, 2010 3:20 PM
  • Sure you are sure!!
    Friday, September 24, 2010 12:03 PM