March 09 2014 display MSSQL DateTime on razor.

Tagged Under : ,
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 retrieve the MSSQL DateTime Data to you application, it will display like below format.

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

Because the DateTime format set in 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.

Make a Comment