Change datetime to date in MVC 3 C#

I'm using the MVC 3 with C# and I have a problem when displaying dates in a dropdown list. Dates are displayed as follow: 4/21/2011 12:00:00 AM, but I just want to format them this way: 4/21/2011, which is exactly the format in my database.

The date attribute in the database is date and not datetime.

The code in the model:

[DisplayFormat(DataFormatString = "{mm/dd/yyyy}", ApplyFormatInEditMode=false)]
public DateTime? booksDate { get; set; }

the controller looks like this:

 var booksDates = from dates in db.Books
                             orderby dates.booksDate
                             select new { dates.booksDate};

          ViewBag.Dates = BooksDates.Distinct();

and finally the code in my view:

<td>Books dates</td>
            <td>@Html.DropDownList("booksDate", new SelectList(ViewBag.Dates as System.Collections.IEnumerable, "booksDate", "booksDate"), "Select a date") </td>

Where i'm wrong ?



You could try to put to use a regular html dropdown and set the html values by hand. I'm sure there's a better way and it might be dirty, but it gets the job done.


ViewBag.Dates = BooksDates.Distinct();


@foreach (var date in ViewBag.Dates)


If I'm not completely off the DataFormatString should look like this:

DataFormatString = "{0:MM/dd/yyyy}"

or even simpler using short date format:


You could extend helper class to do the job in view.

public static class FormatHelper
    public static IHtmlString ToDate( this HtmlHelper helper, DateTime datetime )
        return new HtmlString( datetime.ToShortDateString() );

In view:

@Html.ToDate( Model.Date )

Dont forget to import namespace to view.

As like as in controller:

ViewBag.Date = Convert.ToDateTime(SelectedDate);

in view


output like:


You can do it on pure c# by doing:

string date = Convert.ToDateTime(date).ToShortDateString();

if you are using sql, you can do it this way:

select field1, convert(nvarchar(10), field2, 101) 

from table where //whatever,

let me explain what is all this about:

-field2 is the date containing field. -nvarchar(10) is the type that you will revieve. This hasn't much importance, because it's the type of that you're reciveing ( the result of the convertion). Like it's a select, you don't care about the data type here. It's only for showing the data. The number 10 is the lenght of the string you're getting.

-field2 :

the field that you want to convert ( the date with the hour)

101: it's the style. This tells the field2 that has to print only the month/day/year.

There's a lot of styles, you should google them!

I hope it works.

Need Your Help

I want 2 widgets side-by-side to fill 100% of div

wordpress html widget

In wordpress, I have 2 widgets of different widths which I want to sit side-by-side and fill the entire width of my main content divider (these are not sidebar widgets). I've already got the widgets

Django views / URLS page now loading

python django django-views

Been stuck on this for ages. Currently learning Django and done some bits on other sites. Starting building my own site and getting stuck with the views / URLS.