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.
- Create a new ASP.NET MVC application using the default template
Add a view model:
public class MyViewModel
{
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime Date { get; set; }
}A controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
Date = DateTime.Now,
});
}
}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>
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:
and obviously the result will be formatted as expected:
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
Open File Dialog and Select a File Using Wpf Controls and C#
Post Build Event Execute Powershell
How to Add a String to a String[] Array? There's No .Add Function
Getting All Changes Made to an Object in the Entity Framework
+= New Eventhandler(Method) VS += Method
Differencebetween a C# Reference and a Pointer
Getting Downloads Folder in C#
What Method in the String Class Returns Only the First N Characters
Date Format Yyyy-Mm-Ddthh:Mm:Ssz
How to Generate a Constructor from Class Fields Using Visual Studio (And/Or Resharper)
How to Get User Type C# Syntax Highlighting Working Again in VS 2012 Rc
Query String Not Working While Using Attribute Routing
Display Progress Bar While Doing Some Work in C#
Async Call with Await in Httpclient Never Returns
How to Run a .Net 4.5 App on Xp