1

I have an MVC controller that is called via Ajax. That controller makes a query to a database returning a date (with 00:00:00 as the time value)

In a case, record stored in the SQL Server database has the value of "2019-05-01 00:00:00.000".

I am running this entity framework instructions:

        var periodos = db.Periodo.Where(p => p.PeriodoEliminadoEn == null && p.ClienteId == this.ClienteID);
        var listado = periodos.Select(p => new
        {
            PeriodoId = p.PeriodoId,
            PeriodoFecha = p.PeriodoFecha,
            PeriodoCreadoEn = p.PeriodoCreadoEn,
            PeriodoActualizadoEn = p.PeriodoActualizadoEn,
            PeriodoActivo = p.PeriodoActivo
        });

        GridRowObject grid = new GridRowObject(totalPages, currentPage, totalRecords, listado.ToList());
        return Json(grid, JsonRequestBehavior.AllowGet);

GridRowObject is just this:

public class GridRowObject
{
    public int total { get; private set; }
    public int page { get; private set; }
    public int records { get; private set; }
    public IEnumerable rows { get; private set; }
    public object userdata { get; set; }

    public GridRowObject(int total, int page, int records, IEnumerable rows)
    {
        this.total = total;
        this.page = page;
        this.records = records;
        this.rows = rows;
    }

    public GridRowObject(int total, int page, int records, IEnumerable rows, object userdata)
        : this(total, page, records, rows)
    {
        this.userdata = userdata;
    }
}

When the Ajax is made, the value of "PeriodoFecha" is ""/Date(1556679600000)/"" as I have seen in Chrome develoment tools.

I used this page, https://www.epochconverter.com/, to see real date value and I saw that it is: 1 May 2019 3:00:00 GMT and 30 de abril de 2019 23:00:00 local time.

The fact the page I developed shows the local date, which obviously is incorrect.

The problem is because of time zone problems in my country. According to government, timezone was changed before the change in other years. The server is not aware of that change. So, for the server, timezone is still GMT-3, however, real time zone is GMT-4. For that reason, all visitors' PC's show the date as April the 30th, which is obviously incorrect.

How can I get rid of time zone? I expect that, if the server serializes the date and time using its Json serializer, the date and time should be sent as is, without converting to GMT first.

And the client computer, when using "moment" jquery plugin to convert that json date back to date and time, be converted without considering time zone.

Regards

Jaime

jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • How are you currently configuring asp.net-mvc's `JsonSerializerSettings`? `"/Date(1556679600000)/"` is not the default serialization format for `DateTime` used by Json.NET, you have to use set [`DateFormatHandling = DateFormatHandling.MicrosoftDateFormat`](https://www.newtonsoft.com/json/help/html/DatesInJSON.htm#JavaScriptDateTimeConverter). – dbc May 07 '19 at 22:53
  • Unclear what you're asking. The client/browser is in fact giving you the **correct** date - it's up to you to convert that value ([which is `ms`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)) to your "local date/time" – EdSF May 07 '19 at 23:38
  • @EdSF the problem is that server timezone is GMT-3... Client computers has GMT-4. So, when server transmit time to client, it transmit GMT time. When client convert that time to locsl, resulting time is incorrect. So, the question is simple. How to avoid that GMT conversions. – jstuardo May 08 '19 at 01:26
  • @dbc it seems that default is the way I am receiving. I am not configuring any kind of date format. Just use return Json(object) from the controller. Nothing else. – jstuardo May 08 '19 at 01:29
  • @jstuardo - then what version of [tag:asp.net-mvc] are you using? – dbc May 08 '19 at 02:12
  • @dbc I am using MVC 5.2 – jstuardo May 08 '19 at 03:55
  • It looks like MVC 5.2 is still using `JavaScriptSerializer` not Json.NET, see [this comment](https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc/14591946#comment61397386_14591946) to [Setting the default JSON serializer in ASP.NET MVC](https://stackoverflow.com/q/14591750/3744182). You might check that by temporarily applying [`[ScriptIgnore]`](https://learn.microsoft.com/en-us/dotnet/api/system.web.script.serialization.scriptignoreattribute?view=netframework-4.8) to one of your properties and testing if that prevents serialization. – dbc May 08 '19 at 04:01
  • Time doesn't work that way, and it's not "simple". Your goal of "removing GMT" will not work. End of the day, if your client needs to work with time, then you _must_ account for it - e.g. if my "today" is 5/8/2019 (US Pacific), if your server is in Japan, sending me (client) a date of 5/9/2019 makes no sense - as far as I'm concerned, that's in the future. – EdSF May 08 '19 at 17:14
  • @EdSF Server is in USA, but time is configured with the time in Chile and Chilean time zone. Clients are in Chile, of course, also with Time Zones set to Chile. As I explained, server has incorrect definition of time zone for Chile. In order to solve the problem, I had manually configured time zone for a country different from Chile which is actually GMT-4 (in the server). That way system works, however, when the summer time starts again, I will need to connect to the server, and manually update the time zone again. That is what I want to avoid. – jstuardo May 08 '19 at 20:32

0 Answers0