Format DateTime in C#
33,074
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.
Author by
Dad Daniel
Updated on January 17, 2022Comments
-
Dad Daniel over 2 years
how can I format date to display in the following format 03-Feb-2011
I tried this statement but it did not work properly
string.Format(visitDate.Value.ToShortDateString(), "dd-MMM-yyyy")
-
bleeeah over 13 yearsI meant a "Assuming visitDate is a DateTime? (nullable)"
-
Sean over 13 yearsIt looks like visitDate is DateTime? (nullable), so he'll have to use visitDate == null ? "" : visitDate.Value.ToString("dd-MMM-yyyy") [assuming you want "" if visitDate is null]