I tried to create a view helper which takes a DateTime object and returns a string. If the DateTime object equals a new DateTime(0), the function returns an empty string. Otherwise return a formatted DateTime string. This works so far.
public static string DateTimeOrEmpty(this HtmlHelper htmlHelper, DateTime dateTime)
{
return dateTime.ToString().Equals(new DateTime(0).ToString())
? String.Empty
: dateTime.ToString("{0:g}");
}
The Problem is, that I'd like to pass the format ("{0:g}") as a parameter:
public static string DateTimeOrEmpty(this HtmlHelper htmlHelper, DateTime dateTime, string format)
{
return dateTime.ToString().Equals(new DateTime(0).ToString())
? String.Empty
: dateTime.ToString(format);
}
But it doesn't work properly. If I call the helper from my view
<%: Html.DateTimeOrEmpty(Model.StopDate, "{0:g}") %>
the function with "{0:g}" as parameter for "format", I get something like "{O:n. Chr.}", which is not what I expect