Format DateTime in C#

Solution 1:

visitDate.Value.ToString("dd-MMM-yyyy"); 

Assuming visitDate is a DateTime.

Solution 2:

you have to use:

visitDate.Value.ToString("dd-MMM-yyyy");

Solution 3:

string.format is valid but the parameter order you use are invalid:

DateTime? visitDate = null;
System.Diagnostics.Debug.WriteLine(visitDate == null ? "" : String.Format("{0:dd-MMM-yyyy}", visitDate));

visitDate = DateTime.Now;
System.Diagnostics.Debug.WriteLine(visitDate == null ? "" : String.Format("{0:dd-MMM-yyyy}", visitDate));

That should do the trick.