简体   繁体   中英

Why storing the date like dd-MMM-yyyy is getting converted to dd-MM-yyyy?

I have a very different kind of problem.

I have deployed my mvc application to live server.

It has date time format like this '01/07/2019'.

I have a textbox which is populated from jquery datetimepicker. It populates like this 27/Jul/2019 but in sql table it stores like 2019-07-17 00:00:00.000 and after binding the data from database to textboxfor, it appears like this 27-07-2019.

And upon saving it throws error for all the dates that are greater than 12 as a day eg 13/07/2019 but for 13/Jul/2019 it works good.

how to tackle this?

@Html.EditorFor(model => model.InspectionReport.InspectionDate, new { htmlAttributes = new { @class = "form-control  input-sm pull-right text-box single-line" } })

jquery:

$("#InspectionReport_InspectionDate").datepicker({ dateFormat: 'dd/M/yy' });

class:

[Required]
[Display(Name = "Inspection Date")]
[DisplayFormat(DataFormatString = "{0: dd-MMM-yyyy}")]
public DateTime InspectionDate { get; set; }

In a date field of data type DateTime(2) , a date value is stored as a value with no format .

Thus, to display the value, apply the format you wish, or a default format will be applied.

尝试这个,

  @Html.TextBoxFor(model => model.InspectionReport.InspectionDate, "{0:dd-MMM-yyyy}", htmlAttributes: new { @type="date" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM