0

I have following date time formatted string.

04-AUG-2022

I need to convert it into following date format. 04 August 2022. how can I do that, I tried following code.

string date = "04-AUG-2022";
DateTime d = DateTime.ParseExact(date, "dd-MMMM-yyyy", 
                                 CultureInfo.InvariantCulture);

But it shows,

System.FormatException: 'String '04-AUG-2022' was not recognized as a valid DateTime`

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • The [format string](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings) for abbreviated month names is MMM, not MMMM. `DateTime.ParseExact(date, "dd-MMM-yyyy", CultureInfo.InvariantCulture);` should work – Klaus Gütter Jul 06 '22 at 06:01
  • add below code hopefully is can solve your problem string date = "04-AUG-2022"; DateTime d = Convert.ToDateTime(date); – Sabbir Islam Mukdo Jul 06 '22 at 07:34

3 Answers3

3

For 'AUG' you need MMM not MMMM.

var d = DateTime.ParseExact("04-AUG-2022", "dd-MMM-yyyy", CultureInfo.InvariantCulture);

var d2 = DateTime.ParseExact("04-AUGUST-2022", "dd-MMMM-yyyy", CultureInfo.InvariantCulture);
tymtam
  • 31,798
  • 8
  • 86
  • 126
2

Just do it like this,

string date = "04-AUG-2022";
var formatedDate = DateTime.Parse(date).ToString("dd MMMM yyyy", CultureInfo.InvariantCulture);
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
0

please just do like this,

string date = "04-AUG-2022";
var Updateddate = Convert.ToDateTime(date).ToString("dd MMMM yyyy");
DTemp S
  • 11
  • 2
  • you need to use not tostring cause question is making datetime not string. ==== //Code string date = "04-AUG-2022"; DateTime d = Convert.ToDateTime(date); //end code – Sabbir Islam Mukdo Jul 06 '22 at 07:35