kenp
kenp

Reputation: 500

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 ?

Thanks

Upvotes: 4

Views: 10982

Answers (6)

Paritosh Sarkar
Paritosh Sarkar

Reputation: 1

As like as in controller:

ViewBag.Date = Convert.ToDateTime(SelectedDate);

in view

@Convert.ToDateTime(ViewData["Date"].ToString()).ToShortDateString()

output like:

03/02/2017

Upvotes: 0

diesel
diesel

Reputation: 1

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.

Upvotes: 0

Timothy von Hollen
Timothy von Hollen

Reputation: 135

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.

Controller:

ViewBag.Dates = BooksDates.Distinct();

View:

<select>
@foreach (var date in ViewBag.Dates)
{
    <option>
       @Convert.ToDateTime(date.StartDate).ToShortDateString()
    </option>

}
</select>

Upvotes: 5

Eric Frick
Eric Frick

Reputation: 857

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.

Upvotes: -1

BrokenGlass
BrokenGlass

Reputation: 161012

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

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

or even simpler using short date format:

DataFormatString="{0:d}"

Upvotes: 0

Eric Frick
Eric Frick

Reputation: 857

You can do it on pure c# by doing:

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

Upvotes: -1

Related Questions