locked
MVC save date to format MM/dd/yyyy in entity framework RRS feed

  • Question

  • User-1162256410 posted

    hi. i cant seem to understand why my data saves in a yyyy/MM/dd h:m:s format in database but i declared in my class [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] this. what am i doing wrong? thanks.

    Monday, November 13, 2017 6:33 AM

Answers

All replies

  • User-335504541 posted

    Hi C.Aeon,

    Are you using code first?

    If so, you could try to use ColumnAttribute.

    For example:

    [Column(TypeName="Date")]
    public DateTime Date { get; set; }

    You could refer to the link below for more information:

    https://stackoverflow.com/questions/5658216/entity-framework-code-first-date-field-creation

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 13, 2017 1:39 PM
  • User753101303 posted

    Hi,

    You have to distinguish about the "actual" value which is stored without any format and how it is shown or entered as a string which depends on country conventions (or using some more general format when shown to developers).

    You have the same thing for decimal numbers ie you'll see 1.234 in your db or in your C# code even though some countries are using 1,234 (ie a comma rather than a point as the decimal separator) to show or input this value.

    Monday, November 13, 2017 1:45 PM
  • User-1162256410 posted

    The code given above works. In addition to his answer, kindly check your database data type from datetime to date. I read in one of the answers that and changed it, now everything works perfectly. Thanks again sr

    Tuesday, November 14, 2017 2:28 AM