Change DateTime.Now format to ISO 8601

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    Change DateTime.Now format to ISO 8601

    Hello guys,

    I get this time from DateTime.Now "6/12/2009 11:45:28 AM" and I need to convert it ISO 8601 which is the one that SQL accepts "2009-12-06 11:45:28".

    What can I do?
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    #2
    I found a way to do this

    Code:
    DateTime lastTime = DateTime.Now;
    string x = lastTime.ToString("u").Replace("Z", "");
    but I need to do this in 1 step because I send this time by parameters, i,e:

    Code:
    Insert(DateTime.Now);
    And I would like to send the time through it

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      SQL server takes DateTime objects as a valid parameter. There should be no need to format the output string.

      However, the .ToString() function has overloads which allows you to print out the date time exactly how you want it, check msdn on it

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by raulbolanos
        Hello guys,

        I get this time from DateTime.Now "6/12/2009 11:45:28 AM" and I need to convert it ISO 8601 which is the one that SQL accepts "2009-12-06 11:45:28".

        What can I do?
        Use the ToString method to format it however you like

        Code:
        DateTime.Now.ToString("YYYY-MM-dd hh:mm:ss");

        Comment

        Working...