NullDateToString
public static string NullDateToString(this DateTime? dt, string format = "M/d/yyyy", string nullResult = "") { if (dt.HasValue) return dt.Value.ToString(format); else return nullResult; }Example:
var nullableDate = GetDateWhichCouldBeNull(); Response.Write(nullableDate.NullDateToString("mm/dd/yy", "-no date found-"));
Description
Prints out a nullable datetime's value (if its not null) in the string format specified as a parameter. A final parameter is specified for what to print if the nullable datetime was, in fact, null.
Details
- Author: Graham Peel
- Submitted on: 6-12-2011 20:08:17
- Language: C#
- Type: System.DateTime
- Views: 2866
Double click on the code to select all.