Assign Format of Datetime with Data Annotations

Using Data annotation in Model for DateTime

Your EditorFor() method renders an input with type="date" because of the [DataType(DataType.Date)] attribute, which will generate the browsers HTML-5 datepicker (but this is only supported in Chrome and Edge).

To generate a HTML-5 datetimepicker, you need to render an input with type = "datetime-local" but there is no data annotation attribute for that. Instead you need to generate the type attribute yourself.

@Html.TextBoxFor(m => m.EndTime, "{0:s}", new { @type = "datetime-local", @class = "form-control" })

Note "{0:s}" is shortcut for "{0:yyyy-MM-ddTHH:mm:ss}" and will display the date and time in the browsers culture. Note also that you do not need your [DataType] attribute or the ApplyFormatInEditMode = true property in the [DisplayFormat] attribute since they apply only to EditorFor()

C# MVC How to change format of date in model?

You can create an extension method which works for both datetime and nullable type.

This might help:

//datetime
public static string ToShortDateString(this DateTime date){
return date.ToString("MM/dd/yyyy");
}
//nullable datetime
public static string ToShortDateString(this DateTime? date){
if(date==null){
return null;
}
return date.Value.ToShortDateString();
}

In your code, you can use it like this

@item.Date_Of_Birth.ToShortDateString();

ASP.NET MVC3 datetime format

I suspect the problem is that you haven't specified ApplyFormatInEditMode = true in your attribute, so it's parsing using the default format, but formatting with the format you're specifying.

I'm not an MVC dev by any means, but if you want the same format to be used in both directions, I believe you can just change your attribute to:

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

Note that I've changed hh to HH, as I suspect you want a 24-hour format. I'd actually suggest using yyyy-MM-ddTHH:mm:ss (ISO-8601) as a less ambiguous format for the URL parameter - with a more user-centric display format, of course.

Cultural specific data annotations

This format is culture specific. You must be doing something wrong.

  1. Create a new ASP.NET MVC application using the default template
  2. Add a view model:

    public class MyViewModel
    {
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime Date { get; set; }
    }
  3. A controller:

    public class HomeController : Controller
    {
    public ActionResult Index()
    {
    return View(new MyViewModel
    {
    Date = DateTime.Now,
    });
    }
    }
  4. And a view:

    @using MvcApplication1.Models
    @model MyViewModel

    @Html.DisplayFor(x => x.Date)

Now force the culture in your web.config to some specific culture:

<system.web>
<globalization culture="fr-FR"/>
...
</system.web>

Sample Image

So make sure you set the culture to auto in this case:

<system.web>
<globalization culture="auto"/>
...
</system.web>

Then the browser will send the proper Accept-Language request header that looks like this:

Sample Image

and obviously the result will be formatted as expected:

Sample Image

ASP.net MVC Data Annotations DateTime Default Value

I believe you will have to use the nullable DateTime? type. DateTime cannot be null thus it will always have a value.

Html.EditorFor, data annotations and html attributes for DateTime field

In order to have the DataAnnotations flow through, you must change your Editor Template to use TextBoxFor instead of TextBox, like so:

@ModelType DateTime
@Html.TextBoxFor(Function(m) m, New With {.class = "date"})


Related Topics



Leave a reply



Submit