0

below is purchase class,which contain property as orderdate with type datetime

public class Purchase
{
    public int Id { get; set; }
    public string OrderNo { get; set; }      
    public DateTime OrderDate { get; set; }     
    public string Location { get; set; }
}

when i create object of purchase i want to assign date to it.and for output i want to show format as "dd-MMM-yy" eg. 27-May-14

i tried purchase.OrderDate = Convert.ToDateTime("27-May-13"); but it shows result as

5/27/2013 12:00:00 AM
dcastro
  • 66,540
  • 21
  • 145
  • 155
  • 1
    Well where are you showing it? You've shown the property declaration, but nothing about where it's being displayed. (Searches for DateTime formatting MVC find a *lot* of hits, by the way... did you check those?) – Jon Skeet Apr 05 '14 at 08:28
  • 1
    `DateTime` doesn't have a format. `String` have. You can use `DateTime.ToString(string)` method for string representation of your `DateTime` value. Since you use MVC, you can use `DataFormatString` for your property. – Soner Gönül Apr 05 '14 at 08:29

3 Answers3

2

Something like this in your (View)Model:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString="{0:dd-MMM-yyyy}", ApplyFormatInEditMode=false)]
 public DateTime OrderDate { get; set; }   

and then use Html.DisplayFor() and Html.EditorFor() in Razor.

H H
  • 263,252
  • 30
  • 330
  • 514
1

DateTime has no format but if you want to format it while displaying it to the user using some custom format you can use ToString() method as below

Try This:

var result = purchase.OrderDate.ToString("dd-MMM-yyyy");
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

The class has no format. A DateTime is a point in time defined as a number. The string representation is not done in the DateTime but when you convert it to a string. WHat you use as in put is totally irrelevant. It is not remembered.

So, if you want to show the datetime in a specific format, then format it in the view when you output it. Or set the default formatting in the thread (which will depend on ui culture mostly - unless you do not smart things - selected by the user in his browser settings).

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • i have used mvcgrid in view.and i want to show date in dd-MMM-yy format.but everytime im getting output as 5/27/2013 12:00:00 AM like this in grid view. how to change format of date in mvcgrid. – Sanjivani Gavandi Apr 09 '14 at 12:55
  • Ah, that is a otally different question, is it not? It is also a duplicate to http://stackoverflow.com/questions/7326687/how-to-format-date-in-mvc-web-grid for example. Try using a little google - especially for trivial questions like this. – TomTom Apr 09 '14 at 12:57
  • @Html.Grid(Model).Columns(columns => { columns.Add(c => c.OrderNo).Titled("Order No").SetWidth(200).Sortable(true).Filterable(true); columns.Add(c => c.OrderDate).Titled("Order Date").SetWidth(200).Sortable(true).Filterable(true); }).WithPaging(5) this is my code for mvcgrid. how to change date format using this – Sanjivani Gavandi Apr 09 '14 at 13:01