0

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.

stibay
  • 1,200
  • 6
  • 23
  • 44
  • 1
    You do not want to change the actual value, just the presentation. Google can tell you about Views / Shared / DisplayTemplates used by Html.DisplayFor. – sisve Aug 15 '14 at 07:42
  • You can find the answer here: http://stackoverflow.com/questions/18288675/display-datetime-value-in-dd-mm-yyyy-format-in-mvc4 – Georgi Bilyukov Aug 15 '14 at 07:44

3 Answers3

6

Try to use

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

It would works with EditorFor and DisplayFor helpers

IVAAAN123
  • 557
  • 2
  • 9
0

When you use this property

Convert.ToDateTime(Downtime_start).ToLongDateString()

The revers (Saving to db) should be allowed with hours, second and minutes.

Lali
  • 2,816
  • 4
  • 30
  • 47
0

The solution for this got abit complicated since I had to get this to work with a Javascript Datetimepciker addon. (https://github.com/xdan/datetimepicker) And the format had to be very spesific.

So i ended up creating a helper method.

namespace MvcHtmlHelpers
{
    public static class MvcHtmlHelpers
    {
        public static string ToString(this DateTime inputDate, bool FixTime)
        {
            if (FixTime)
            {
                string returnString;

                if (inputDate == DateTime.MinValue)
                {
                    returnString = "N/A";
                }
                else
                {
                    returnString = inputDate.ToShortDateString() + ' ' + inputDate.ToShortTimeString();
                }
                return returnString;
            }
            return inputDate.ToString();
        }
    }
}

My form has one "viewing stage" and one "edit-mode stage" which is controlled by a edit button. And I am overlaoding the .Value.ToString(true) method to use the helper method to format the date correctly.

<section class="col col-4">
    <label class="label">@Html.DisplayNameFor(model => model.Downtime_start)</label>
    <label class="input edit-case">
        <i class="icon-append fa fa-calendar"></i>
        <input type="text" id="Downtime_start" value="@Model.Downtime_start.Value.ToString(true)">
    </label>
    <label class="input state-disabled view-case">
        <i class="icon-append fa fa-calendar"></i>
        <input type="text" id="lbl_Downtime_start" disabled="disabled" value="@Model.Downtime_start.Value.ToString(true)">
    </label>
</section>
stibay
  • 1,200
  • 6
  • 23
  • 44