c# - Globally convert UTC DateTimes to user specified local DateTimes -


i storing datetime fields utc time. when user requests web page, take preferred local timezone (and not local timezone of server machine) , automatically display datetime fields in web forms local dates.

of course, apply conversion on every datetime.tostring() call in every form or implement helper utility time consuming task, , there 3rd party components tricky configure custom datetime display templates.

essentially, make datetime class behave follows:

from moment on web request,  whenever code calls datetime.tostring(), convert local time          using timezone offset given @ beginning of web request,  if possible, please keep .net core library datetime.tostring() calls intact         (i don't want mess event logging timestamps etc.) 

is there way it?

btw, using asp.net mvc 4, if matters.

you can't directly asked for, suggest alternatives. nicholas pointed out, there nothing in http give time zone directly.

option 1

  • first, decide type of time zone data want work with. there 2 different types available, either microsoft time zones can access timezoneinfo class, or iana/olson time zones rest of world uses. read here more info. recommendation latter, using implementation provided nodatime.

  • then determine time zone want convert to. should allow user setting somewhere pick time zone.

    • you might show drop-down list pick 1 of several time zones, or might more useful, display map of world can click select time zone. there several libraries can in javascript, favorite this one.

    • you might want guess default time zone use, can close accurate possible before pick list (or map). there great library called jstimezonedetect. interrogate browser's clock , make best guess assumption of time zone might be. good, still guess. don't use blindly - use determine starting point. update can moment.tz.guess(), in moment-timezone component of moment.js.

  • now know time zone of user, can use value convert utc datetime values local time zone. unfortunately, there nothing can set on thread that. when change system time zone, global processes , threads. have no choice pass time zone each , every place sending back. (i believe main question.) see almost duplicate here.

  • before convert string, need know user's locale (which can request.userlanguages value). can assign current thread, or can pass parameter datetime.tostring() method. doesn't time zone conversion - makes sure numbers in correct position, using correct separators, , appropriate language names of weekdays or months.

option 2

don't convert local time on server @ all.

  • since said working utc values, make sure .kind property utc. should when load database, if have can manually:

    mydatetime = datetime.specifykind(mydatetime, datetimekind.utc); 
  • send browser pure utc, in invariant format iso8601. in other words:

    mydatetime.tostring("o");  // example:  "2013-05-02t21:01:26.0828604z" 
  • use javascript on browser parse utc. automatically pick local time settings of browser. 1 way use built-in date object in javascript, this:

    var dt = new date('2013-05-02t21:01:26.0828604z'); 

    however, work in newer browsers support iso-8601 format. instead, recommend using moment.js library. consistent across browsers, , has better support iso dates, , localization. plus lot of other useful parsing , formatting functions.

    // pass value server var m = moment('2013-05-02t21:01:26.0828604z');  // use 1 of formats supported moment.js // locale-specific "long date time" format. var s = m.format('llll'); 

the advantage of option 1 can work times in time zone. if can ask user timezone dropdown list, need not use javascript.

the advantage of option 2 browser of work you. best way go if you're sending out raw data, such making ajax calls webapi. however, javascript aware of utc , browser's local time zone. doesn't work if need convert other zones.

you should aware if choose option #2, may affected flaw in design of ecmascript 5.1. comes play if working dates covered different set of daylight saving time rules in effect. can read more in question, , on blog.

it easier if had time zone information in http headers, unfortunately don't. these lot of hoops jump through, it's best way have both flexibility , accuracy.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -