Send UTC Datetime Javascript to C#

Asked

Viewed 321 times

2

I have an ASP.Net MVC project as follows:

In my view I have a Javascript variable that keeps a date:

var hoje = new Date();

In my controller have a action which receives via AJAX the value of the Javascript variable in a type C# variable DateTime.

public ActionResult ObterData(DateTime hoje)
{
   ...
   var utc = // Gostaria de obter o utc (Fuso Horário) da variável hoje como veio da View.
   ...
}
  • Do you want the time in UTC or do you want to know what the TimeZone hers?

  • I want to know her Timezone, UTC has nothing to do with it?

1 answer

2


UTC is the universal standard time, the so-called GMT, is zero time, the time that is independent of where it is.

This time zone information is not available on type DateTime, therefore it cannot be obtained. I think it gets worse coming from the customer.

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

The only way is to have additional information to store this. It can be separated or create a new type that encapsulates the time and time zone. If the Javascript code does not get the local time information (and of course this is not something you can trust) and does not send it to the server, there is no solution, unless you consider the server time to be the same as the client, but it is a presumption that may be wrong. You have to send the time difference from the local time to the GMT time (UTC). With this additional information it is possible to make calculations on the local time of the client.

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.

  • Well, the purpose of short form is that when a date is sent from a customer who is running the application in the Greenwich Meridian I can get on Controller the following valor = 0:00, but if the client is in Brasilia valor = -03:00. How would this value be called in technical terms? The goal is precisely to make conversions later with these values.

  • 1

    First, you know you can’t trust this information, right? You have no control over what comes from the customer. You can not even guarantee that comes an information if the time is UTC or Local and this is the least you need to make any calculation in this sense. Of course, any data could be wrong. But there is no other way but to take the time zone in the client, with JS, save in some field that will be sent to the controller together with the date. You can’t just get the date. So this -03:00 has to be sent by the client somehow.

  • On the client side, you can use getTimezoneOffset() to determine the difference between local time and UTC: new Date().getTimezoneOffset();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.