Convert datetime from one format to another format RRS feed

  • Question

  • I want to convert a datetime from one foramt to another one...

    outputDAteString1 = "2013/02/26 03:37:11.000 AM";
    string sDt = Convert.ToDateTime(outputDateString1).ToString("d/MM/yyyy h:mm:ss tt");
      dtToSelect = Convert.ToDateTime(sDt);

    It throws an exception 'String was not recognized as a valid date time"

    But the same code works fine, if the outputDateSTring1 = "2013/02/4 03:37:11.000 AM"

    I noticed that this fails, if the 'date' part crosses the number 12. I wonder why it fails for the datepart crossing 12.

    Please help me to fix this!
    Tuesday, March 5, 2013 8:43 AM


  • Use DateTime.Parse() method to convert from string date to time.

    string  outputDAteString1 = "2013/02/26 03:37:11.000 AM";
               DateTime outputDAte = DateTime.Parse(outputDAteString1);
               string sDt = outputDAte.ToString("d/MM/yyyy h:mm:ss tt");


    Tuesday, March 5, 2013 10:31 AM