C# Tips

C# Tip Article

Convert long date to short date

How to convert long date string to short date strong?

Let's say we got a date string "Thursday, December 03, 2015" and want to change it to "12/3/2015."

One easy way of doing it is convert long date string to DateTime type and get short date string from it. Here is a sample code.

string longDate = DateTime.Now.ToLongDateString();
Debug.WriteLine(longDate); //Thursday, December 03, 2015

DateTime dt = Convert.ToDateTime(longDate);
string shortDate = dt.ToShortDateString();
Debug.WriteLine(shortDate); //12/3/2015