Pregunta

I've spent quite a bit of time on stackoverflow and the internet trying to determine what has been done in my situation. I am building a centralized line of business website that could span multiple timezones. Based on some valuable information here I have a plan in place to handle this.

Now to the issue I don't know how to solve. Kendo UI uses JSON to pass information back and forth and as far as I can tell the JSON.net serializer cannot serialize a date to a specific time zone. I can serialize the date as UTC or have JSON.net automatically convert it to local time using the DateTimeZoneHandling setting but that would be local time on the server. From looking at the JSON.net code, I'm not sure I could even write a converter to do what I wanted. So far now if I send the date as UTC through JSON.net and Kendo automatically converts the time to the browser's local timezone.

Assuming User Profile timezone = Browser Timezone seems a little scary for me and I wanted to see what stackoverflow suggests I do. Thank you in advance.

Technology

ASP.net MVC 5

Entity Framework 6

Telerik Kendo UI Controls

JSON.net

¿Fue útil?

Solución 3

I was finally able to find this answer here https://stackoverflow.com/a/11968692/3412859 that will allow me to write my own JSONconverter and convert to a specific time zone before it is serialized. I now have all of the dates being converted on the server side based on the timezone setting in the user profile.

Otros consejos

Unfortunately I don't know many of the tools you are using, but I know how I would handle timezones.

I would use UTC everywhere, except when displaying times (or rather: datetimes) and in user input.

I would let the browser's JavaScript do the conversion in output, using the browser's timezone. In input, the conversion can be done either client side or server side. In the latter case the timezone must be part of the information sent from the browser to the server. The UTC timezone on the servers should be set at a level as low as possible, and, if possible, in all components (OS, web server, DB...).

A user's preferred timezone is nonetheless useful for all cases where the timezone is not known (e.g., in email communications - but remember to always have the timezone in time formats).

There are times (no pun intended...) when the above is not easy or does not apply. E.g., a schedule (calendar), to which the user attaches the timezone they have in their head, not necessarily the browser's one. Another case is when the user's timezone has to be known for some computation that can not easily be done on the browser - in this case the server will have to get the timezone from the browser first. But most of the time (still no pun intented...) you can happily do without timezones (with UTC) except immediately before showing something to the user or immediately after getting something from the user.

Regarding your last paragraph: you will very soon run into trouble if you assume "user profile timezone = browser timezone".

Some last words of advice:

  • "Timezone" is an ambiguous term. It may refer to a fixed offset from UTC (like UTC+1), or to the rules for calculating this offset for any instant in a given area (like the timezone of Rome, Italy). Always use the second meaning (e.g., don't use the current offset for a date in the future or past, but correctly convert datetimes back and forth), and remember that both can change during a user's session.
  • Be careful not to handle "pure" dates as the midnight of the same day (and not as noon either - people in Samoa will thank you), because then the date can change when the timezone changes. In other words, don't attach time information to pure dates.

P.S.: By following a link of yours, I was happy to find that I totally agree with the closing remark of this answer (by someone with more SO reputation than me)

You certainly can pass UTC to the browser and let it convert, but you have to be aware of the potential for inaccuracies. You also can convert server-side and pass a DateTimeOffset in your JSON, but you may need to render it yourself, using something like moment.js.

In the end, what you should do is entirely up to you. Many use cases are similar, but there are plenty of acceptable different paths depending on your exact needs.

Your question is very broad the way you have asked it. What parts are you most concerned with? Kendo? EF? JavaScript? Client-side time zone conversion? Formatting? Could you refine it or provide some context to the type of things your date/time values are representing? Otherwise, you may get some useful tidbits, but it will be hard to provide a definitive answer.

(Oh, and you might be interested in this.)

I had this problem , too.

It works for me.

I translated the time format to

 Start=2016-4-9T8:30:00.000Z


 End=2016-4-9T13:30:00.000Z

in background or database.

By doing this,local time zone is useful.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top