I’m currently using C# and I want to convert a string like "2022-01-15 18:40:30" to a DateTime object with this format "15-01-2022 18:40:30". Below is what I’ve tried.
string stringDate = "2022-01-15 18:40:30";
string newStringDate = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy HH:mm:ss");
DateTime newDateFormat = DateTime.ParseExact(newStringDate, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
But the result i keep getting is "2022-01-15T18:40:30"
Any help would be appreciated
4
Answers
The DateTime object by itself does not have a specific "format".
The string representation gets created when you call the .ToString() function.
There are multiple overloads of the ToString function with which you can specify the format.
try this style
Try this one:
As others pointed out, you have a internal data value for DateTime.
So, FOR SURE we suggest that you convert the string into that internal format.
Once you do that, then you are free to output that internal date time value to ANY kind of format and output you want.
Thus, we have this:
And output is this: