none
Convert Datetime to String in C# RRS feed

  • Question

  • Hello,

    I want to convert Datetime =5/24/2018 6:04:09 AM to 24 May 2018 6:04:09 AM in String format . Iam trying with below code but getting error as 'String was not recognized as a valid DateTime.' 

    Convert.ToString(DateTime.ParseExact(Convert.ToString(Datetime), "dd/mm/yyyy", CultureInfo.InvariantCulture)),

    Please suggest some code how to convert to 24 May 2018 6:04:09 AM  in this format in string.


    Krishna

    Friday, May 25, 2018 3:35 AM

Answers

  • You shouldn't need all those conversions. Just format the DateTime you start with.

             DateTime now = DateTime.Now;
             string asString = now.ToString("dd MMMM yyyy hh:mm:ss tt");
             Console.WriteLine(asString);

    • Proposed as answer by DmitryFomin Friday, May 25, 2018 6:23 AM
    • Marked as answer by Dkrishna Friday, May 25, 2018 7:27 AM
    Friday, May 25, 2018 3:54 AM