Send UTC from DateTime Javascript to C #

1

I have an ASP.Net MVC project as follows:

In my view I have a JavaScript variable that holds a date:

var hoje = new Date();
In my controller I have an action which receives via AJAX the value of the JavaScript variable in a C # variable of type DateTime .

public ActionResult ObterData(DateTime hoje)
{
   ...
   var utc = // Gostaria de obter o utc (Fuso Horário) da variável hoje como veio da View.
   ...
}
    
asked by anonymous 03.09.2015 / 16:37

1 answer

2

UTC is the universal default time, called GMT, is time zero, the time that does not depend on where you are.

This time zone information is not available in type DateTime , so it can not be obtained. I think it gets worse from the client.

In general you should treat the date as UTC. Eventually you can convert to local time. If you save as local time (this is possible with DateTimeKind ), you can find the difference for UTC. Maybe this difference will work for something but it does not say what time zone it is.

The only way is to have additional information that holds this. You can either separate or create a new type that encapsulates time and time zone. If the JavaScript code does not get the local time information (and this is not something you can trust, of course) and do not send it to the server, there is no workaround. You must set the time difference from local time to GMT (UTC). With this additional information it is possible to make calculations over the client's local time.

You can even use a library like NodaTime that has a more sophisticated type. But the information coming from the client needs to be compatible.

    
03.09.2015 / 17:13