In MVC razor, I am putting current date in the database like this..
model.Returndate = DateTime.Now.Date.ToShortDateString();
Since the database field is a datetime datatype and I am converting the current date to string format, this is not working.. how can I do this? I am doing the string format because I want the date in mm/dd/yyyy format and not in mm/dd/yyyy hh:mm:ss time format..
EDIT:
In the controller I have
var model = new ViewModel();
model.ReturnDate = DateTime.Now;
return PartialView("PartialView", model);
In the partialview, I have
@Html.EditorFor(model => model.Returndate)
This is where its displaying the date as Date and Time together... I want just the date to be displayed. Not the time. I hope this edit explains better.
This question is related to
c#
asp.net-mvc
asp.net-mvc-3
datetime
Just had to deal with this scenario myself - found a really easy way to do this, simply annotate your property in the model like this:
[DataType(DataType.Date)]
public DateTime? SomeDateProperty { get; set; }
It will hide the time button from the date picker too.
Sorry if this answer is a little late ;)
The date/time in the datebase won't be a formatted version at all. It'll just be the date/time itself. How you display that date/time when you extract the value from the database is a different matter. I strongly suspect you really just want:
model.Returndate = DateTime.Now.Date;
or possibly
model.Returndate = DateTime.UtcNow.Date;
Yes, if you look at the database using SQL Server Studio or whatever, you'll now see midnight - but that's irrelevant, and when you fetch the date out of the database and display it to a user, then you can apply the relevant format.
EDIT: In regard to your edited question, the problem isn't with the model - it's how you specify the view. You should use something like:
@Html.EditorFor(model => model.Returndate.Date.ToString("d"))
where d
is the standard date and time format specifier for the short date pattern (which means it'll take the current cultural settings into account).
That's the bit I've been saying repeatedly - that when you display the date/time to the user, that's the time to format it as a date without a time.
EDIT: If this doesn't work, there should be a way of decorating the model or view with a format string - or something like that. I'm not really an MVC person, but it feels like there ought to be a good way of doing this declaratively...
I had some problems with the other solutions on this page, so thought I'd drop this in.
@Html.TextBoxFor(m => m.EndDate, "{0:d}", new { @class = "form-control datepicker" })
So you only need to add "{0:d}" in the second parameter and you should be good to go.
You can apply custom date time format using ToString
like:
DateTime.Now.Date.ToString("MM/dd/yyyy");
Further reading on DateTime.ToString
formats pattern can be found here and here.
Edit: After your question edit, Just like what other suggested you should apply the date format at your view:
model.Returndate = DateTime.Now.Date;
@Html.EditorFor(model => model.Returndate.Date.ToString("MM/dd/yyyy"))
You could simply convert the datetime. Something like:
@Convert.ToString(string.Format("{0:dd/MM/yyyy}", Model.Returndate))
You can modify your ViewModel as below:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
If you have a for loop such as the one below.
Change @item.StartDate to @item.StartDate.Value.ToShortDateString()
This will remove the time just in case you can't annotate your property in the model like in my case.
<table>
<tr>
<th>Type</th>
<th>Start Date</th>
</tr>
@foreach (var item in Model.TestList) {
<tr>
<td>@item.TypeName</td>
<td>@item.StartDate.Value.ToShortDateString()</td>
</tr>
}
</table>
This works if you want to display in a TextBox:
@Html.TextBoxFor(m => m.Employee.DOB, "{0:dd-MM-yyyy}")
I am not sure in Razor but in ASPX you do:
<%if (item.Modify_Date != null)
{%>
<%=Html.Encode(String.Format("{0:D}", item.Modify_Date))%>
<%} %>
There must be something similar in Razor. Hopefully this will help someone.
Include Data Annotations like DisplayFormat and ApplyFormatInEditMode to have the desired output.
[Display(Name = "Bill Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime BillDate { get; set; }
Now your Bill date will show like.
Source: Stackoverflow.com