-1

I have a DateTime object in the format of 6/07/2016, and I want to change it to be in the format of 6th July 2016.

How can I do this in C#? Been looking around but only seem to find ways to turn strings into DateTime in this format. What if I already have a DateTime object? Convert to string first?

Thanks

magna_nz
  • 1,243
  • 5
  • 23
  • 42

2 Answers2

5

You need to pass in the string format in the .ToString call, referencing the custom date and time format specifier page to see which specifiers you need.

string timeString = timeToRun.ToString("d MMMM yyyy")

This will give you 6 July 2016, if you want 6th instead of 6 you need a custom format provider as specified in this question and answer

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
2

I think this code piece can help you.

This question and answer also help me to understand it

ChangeIt(DateTime.Parse("16/07/2016"));

public static string ChangeIt(DateTime date)
{
     switch(date.Day)
     {
        case 1:
        case 21:
        case 31:
           return date.ToString("d'st' MMMM yyyy");
        case 2:
        case 22:
           return date.ToString("d'nd' MMMM yyyy");
        case 3:
        case 23:
           return date.ToString("d'rd' MMMM yyyy");
        default:
           return date.ToString("d'th' MMMM yyyy");
      }
}
Community
  • 1
  • 1
Ekin Yücel
  • 112
  • 5
  • 7