March 09 2014

ASP.net display MSSQL DateTime on razor.

Tagged Under : ,

asp.net
MSSQL store DateTime format is standard. But sometimes we need change the format to display it to user, let them more easy read and understand which Datetime format was set in our Application.

If use ASP.net retrieve the MSSQL DateTime Data to you application, it will display like below format.
asp.net

But, when validate the DateTime with ASP.net it will return an error.
asp.net

Because the DateTime format set in ASP.net was “06-03-2014 12:00:00 AM”. But now we just need to display Date only not need time.

To let it display properly on our application. We can set the DateTime format on our Model file. Open the Model file, and then set it like below example:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime CreateDate { get; set; }

Refresh the page, you will found that the DateTime field already change the format as you set. And you not need change “March” to “03” to pass the validate. asp.net

Make a Comment

You must be logged in to post a comment.