locked
DateTime Conversion RRS feed

  • Question

  • Hi,

    I have DateTime in the format 4/20/2009 02:22:03 PM.

    I want to convert it as 4/20/2009 14:22 . The value should be in DateTime data type only.

    Please help.


    Thanks
    Ashok

    Monday, April 20, 2009 9:07 AM

Answers


  • // this will converts to desired correct format your DateTime
    string myString = myOldDateTime.ToString("M/dd/yyyy hh:mm");
    
    // this will converts your text FROM desired format to a DateTime
    myDateTime = DateTime.ParseExact(mytext, "M/dd/yyyy hh:mm", Application.CurrentCulture);



    Best regards, Sergiu
    • Proposed as answer by Sergiu Dudnic Monday, April 20, 2009 9:44 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:03 PM
    Monday, April 20, 2009 9:26 AM

All replies


  • // this will converts to desired correct format your DateTime
    string myString = myOldDateTime.ToString("M/dd/yyyy hh:mm");
    
    // this will converts your text FROM desired format to a DateTime
    myDateTime = DateTime.ParseExact(mytext, "M/dd/yyyy hh:mm", Application.CurrentCulture);



    Best regards, Sergiu
    • Proposed as answer by Sergiu Dudnic Monday, April 20, 2009 9:44 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:03 PM
    Monday, April 20, 2009 9:26 AM
  • Try the following code:
    DateTime dt = DateTime.Parse("4/20/2009 02:22:03 PM");
    Console.WriteLine(dt.ToString("M/dd/yyyy HH:mm"));

    Shuhai Shen - http://leonax.net
    Monday, April 20, 2009 9:27 AM