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
CodePudding user response:
DateTime date = Convert.ToDateTime("2022-01-15");
DateTime time = Convert.ToDateTime("18:40:30");
DateTime dt = Convert.ToDateTime(date.ToShortDateString() " " time.ToShortTimeString());
try this style
CodePudding user response:
Try this one:
string stringDate = "2022-01-15 18:40:30";
Console.WriteLine((DateTime.Parse(stringDate)).ToString("dd-MM-yyyy HH:mm:ss"));
CodePudding user response:
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.