You can customize the extension method, although using IFormatProvider as suggested by Lucero would probably be the appropriate approach. The extension method is compared with the Date property of Date , which returns the date when the time component is set to midnight. It would be like this:
public static class Extensions { public static string ToCustomFormat(this DateTime date) { if (date.TimeOfDay < TimeSpan.FromMinutes(1)) { return date.AddDays(-1).ToString("MM/dd/yyyy") + " 24:00"; } return date.ToString("MM/dd/yyyy H:mm"); } }
Then call it using:
var date = DateTime.Parse("1/27/2010 0:00"); Console.WriteLine(date.ToCustomFormat());
EDIT: Updated for comments.
Ahmad mageed
source share