What is the best way to handle time zones with Javascript -
i have authenticated user given time zone, e.g. "berlin, gmt+1". sake of question let's have in global scope:
var timezone = "berlin"; var gmtdistance = 1; what best solution have date-related js behave accordingly, meaning if create new date object take timezone account.
i thought it'd pretty straightforward, don't seem find perfect way on google/so. privilege answer doesn't need external library.
my preference store dates on server side using utc time, , when handling data coming via ajax calls, create global handler parsing.
the following example allows use:
app.ajax({ url: '/my/post/url', data: { myproperty: 'myvalue' }, success: function (data, status, xhr) { // stuff here... }, error: function (xhr, settings, error) { // stuff here... } }); but, pre-parses returned values in "success" function's "data" element fixing dates utc time local timezone. aware - after doing this, if further process data, need un-fix before sending server, or you'll posting offset.
var app = window.app = $.extend(true, {}, app, { // creating namespace app safe across multiple files. ajax: function (options) { var defaultsettings = { type: 'post', async: true }; // capture settings. var settings = $.extend(true, {}, defaultsettings, options); // install our general handlers; if (settings.success) { settings.success = function (data, textstatus, jqxhr) { app.onpostsuccess(data, textstatus, jqxhr, options.success); } } else settings.success = app.onpostsuccess; if (settings.error) { settings.error = function (jqxhr, ajaxsettings, thrownerror) { app.onposterror(event, jqxhr, ajaxsettings, thrownerror, options.error); } } else settings.error = app.onposterror; $.ajax(settings); }, onpostsuccess: function (data, textstatus, jqxhr, fn_after) { // generalized success handling here. // fix dates. var fixeddata = app.fixdate(data); // call other handler that's been specified. if (typeof fn_after === 'function') fn_after(fixeddata, textstatus, jqxhr); }, onposterror: function (jqxhr, ajaxsettings, thrownerror, fn_after) { // generalized error handling here. // call other handler that's been specified. if (typeof fn_after === 'function') fn_after(jqxhr, ajaxsettings, thrownerror); }, fixdate: function (obj) { var fixed = obj; if (typeof obj == 'string' && obj.indexof('\/date(') == 0) { // microsoft date "/date(12345678)/" - convert real date. fixed = new date(parseint(fixed.substr(6, fixed.length - 8), 10)); } if (typeof fixed === 'object') { if (fixed.gettimezoneoffset) { // if value date, apply timezone correction. var = new date(); var offset = now.gettimezoneoffset(); // # of minutes gmt. fixed = new date(fixed.gettime() + offset * 60000); // updates value based on offset. } else { // otherwise, update each of properties. // fixes objects dates properties, recursively. $.each(fixed, function (index, value) { fixed[index] = app.fixdate(value); }); } } return fixed; } }); all of set-up this. if handle things dates within onpostsuccess, can ensure in right format - , in right timezone.
whether or not use above ajax methods, can use fixdate method follows:
var myobj = { mydate: "\/date(12345678)\/" }; console.log('before: ', myobj.mydate); myobj = app.fixdate(myobj); console.log('after: ', myobj.mydate); to see example in action, check out following jsfiddle:
http://jsfiddle.net/troyalford/tbnvv/
note: includes ajax bits - aren't used in example - there completeness.
Comments
Post a Comment