How to Convert long date to short date in C#

By FoxLearn 11/10/2024 2:41:55 AM   48
In C#, you can convert a long date string (e.g., "Monday, October 10, 2024") to a short date string (e.g., "10/10/2024") using the DateTime class and its formatting capabilities.

To convert a long date string to a short date string in C#, one simple approach is convert the long date string into a DateTime object and then get short date string from it.

string longDate = DateTime.Now.ToLongDateString();
Console.WriteLine(longDate); //Sunday, November 10, 2024

DateTime dt = Convert.ToDateTime(longDate);
string shortDate = dt.ToShortDateString();
Console.WriteLine(shortDate); //11/10/2024