I have several DateTime fields in my database. Normaly I have just created this in my model like this:
[Display(Name = "Downtime start")]
public DateTime? Downtime_start { get; set; }
Note: It has to be nullable.
So this works fine. But the problem is that it also show the seconds everywhere, which is abit annoing.
So can't I just fix this by doing some parsing in my Model? Something like this?
private string Parsed_Downtime_start;
[Display(Name = "Downtime_start")]
public DateTime? Downtime_start
{
get
{
if (Parsed_Downtime_start != null)
{
return DateTime.Parse(Parsed_Downtime_start);
}
else
{
return null;
}
}
set
{
if (value != null)
{
Parsed_Downtime_start = value.Value.ToString("MM/dd/yyyy hh:mm tt");
}
else
{
Parsed_Downtime_start = null;
}
}
}
This doesn't really change anything. But if I do it the other way and save it to the DB as string and parse it out as DateTime it works fine. But I dont really want to save the DateTimes in the DB as String.