-2

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 e.g. 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; }
CSDev
  • 3,177
  • 6
  • 19
  • 37
Stacky
  • 57
  • 1
  • 8
  • Have you checked the culture info that you've got set in IIS? We had a similar issue where dates were being parsed using the wrong culture. – Clint Jul 01 '19 at 15:49
  • how to check that? – Stacky Jul 01 '19 at 15:51
  • isn't there any date format that won't need any further settings? – Stacky Jul 01 '19 at 15:52
  • Try here: https://stackoverflow.com/questions/1100061/how-to-configure-invariant-culture-in-asp-net-globalization – Clint Jul 01 '19 at 15:52
  • 1
    Web servers are generally set to run in UTC timezone and US (or invariant) cultures. You can set your code to always run in whatever culture you require (somewhere in startup), or you can write code that allows the user of the web site to decide which culture they would prefer. Supporting multiple timezones/cultures is not an insignificant task. – Neil Jul 01 '19 at 16:01
  • The error ir because your sql server and the PC you are developing are not storing the dates the same way. You are trying to send something like (dd-MM-yyyy) but your sql server expects another string (perhaps MM-dd-yyyy, or yyyy-MM-dd). – Antonio Veneroso Contreras Jul 01 '19 at 16:09
  • Check [this page](https://tableplus.io/blog/2018/09/ms-sql-server-how-to-get-date-only-from-datetime-value.html), it will help you. – Antonio Veneroso Contreras Jul 01 '19 at 16:16

2 Answers2

3

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.

Gustav
  • 53,498
  • 7
  • 29
  • 55
0

Try this,

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