0

I have a Kendo UI MVC grid on a page. In this grid are three date columns. The dates (no time value) are stored in the database as local time (not UTC). These columns are defined in the grid as follows:

            columns.Bound(p => p.CloseoutDate).ClientTemplate("#= moment(CloseoutDate).format('MM/DD/YYYY') #").Width(120);
            columns.Bound(p => p.BeginDate).Title("Begin Date").ClientTemplate("#= moment(BeginDate).format('MM/DD/YYYY') #").Width(110);
            columns.Bound(p => p.EndDate).Title("End Date").ClientTemplate("#= moment(EndDate).format('MM/DD/YYYY') #").Width(110);

You can see I'm using moment.js to do my date formatting. However, it seems that moment is assuming these dates are UTC dates and converting them to local time. When the grid is displayed, the dates look as follows: enter image description here

All these dates are off by one day. For example, the first row should be 1/15/2010, 1/1/2010, 1/15/2010. Is this normal moment behavior, and can it be changed?

Also, CloseoutDate, BeginDate and EndDate are actual C# datetime values. In the SQL Server database they are stored as dates.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • Maybe try moving `format` into the `moment(DateField)` e.g. `moment(CloseoutDate, 'MM/DD/YYYY')`. Source from [this question](https://stackoverflow.com/a/37278220/6224482). – Sandman Jun 09 '17 at 12:21
  • Which is the value of `CloseoutDate`, `BeginDate` and`EndDate`? Are these values strings? In this case you can use [`moment(String, String)`](http://momentjs.com/docs/#/parsing/string-format/). – VincenzoC Jun 09 '17 at 12:25
  • @VincenzoC - CloseoutDate, BeginDate and EndDate are actual C# datetime values. In the database they are stored as dates. – Randy Minder Jun 09 '17 at 12:31
  • @RandyMinder unfortunately I don't know enough about C#, what is passed to moment constructor? – VincenzoC Jun 09 '17 at 12:33
  • @VincenzoC is there any difference between your comment and mine? – Sandman Jun 09 '17 at 14:43
  • @Sandman they are quite similar, but my main goal was to know about variables' type from the OP. – VincenzoC Jun 09 '17 at 14:52

2 Answers2

0

This fixed the issue:

            columns.Bound(p => p.CloseoutDate).ClientTemplate("#= moment.utc(CloseoutDate,'MM/DD/YYYY').format('MM/DD/YYYY') #").Width(120);
            columns.Bound(p => p.BeginDate).Title("Begin Date").ClientTemplate("#= moment.utc(BeginDate,'MM/DD/YYYY').format('MM/DD/YYYY') #").Width(120);
            columns.Bound(p => p.EndDate).Title("End Date").ClientTemplate("#= moment.utc(EndDate,'MM/DD/YYYY').format('MM/DD/YYYY') #").Width(120);

Basically using the moment library and the UTC constructor.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
-1

u can format date in column

{ field: "beginDate", title: "Begin Date", format: "{0:yyyy-MM-ddTHH:mm:ss}",parseFormats: "{0:yyyy-MM-ddTHH:mm:ss}"}
DraganB
  • 1,128
  • 2
  • 10
  • 26