Benim tercih UTC saati kullanarak sunucu tarafında tüm tarihleri saklamaktır ve AJAX çağrıları yoluyla geri geliyor verilerinin işlenmesi am bazı ayrıştırma yapan bir küresel işleyicisi oluşturun.
app.ajax({
url: '/My/Post/Url',
data: {
MyProperty: 'MyValue'
},
success: function (data, status, xhr) {
// Do stuff here...
},
error: function (xhr, settings, error) {
// Do stuff here...
}
});
Ama, önceden ayrıştırır yerel saat dilimine UTC kez tarihleri düzelterek "başarı" işlevin "veri" öğesinin içindeki herhangi iade değerleri:
Aşağıdaki örnek sadece kullanmasına olanak sağlar. Dikkatli olun - bunu yaptıktan sonra, verileri daha fazla işliyorsanız, sunucuya geri göndermeden önce BM'yi düzeltmeniz gerekecek ya da ofset ile geri gönderileceksiniz.
var app = window.app = $.extend(true, {}, app, {
// Creating a namespace for my app which is safe across multiple files.
ajax: function (options) {
var defaultSettings = {
type: 'POST',
async: true
};
// Capture the 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) {
// Do my generalized success handling here.
// Fix Dates.
var fixedData = app.FixDate(data);
// Call any other handler that's been specified.
if (typeof fn_after === 'function')
fn_after(fixedData, textStatus, jqXHR);
},
OnPostError: function (jqXHR, ajaxSettings, thrownError, fn_after) {
// Do my generalized error handling here.
// Call any 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 to real date.
fixed = new Date(parseInt(fixed.substr(6, fixed.length - 8), 10));
}
if (typeof fixed === 'object') {
if (fixed.getTimezoneOffset) {
// If the value is a date, apply timezone correction.
var now = new Date();
var offset = now.getTimezoneOffset(); // # of minutes from GMT.
fixed = new Date(fixed.getTime() + offset * 60000);
// This updates the value based on the offset.
} else {
// Otherwise, update each of its properties.
// This fixes objects with dates for properties, recursively.
$.each(fixed, function (index, value) {
fixed[index] = app.FixDate(value);
});
}
}
return fixed;
}
});
Tüm bu ayarlara ulaşmak için. Artık OnPostSuccess içinde tarihler gibi şeylerle başa çıkıyorsanız, her zaman doğru biçimde ve her zaman doğru saat diliminde olduklarından emin olabilirsiniz.
var MyObj = {
MyDate: "\/Date(12345678)\/"
};
console.log('Before: ', MyObj.MyDate);
MyObj = app.FixDate(MyObj);
console.log('After: ', MyObj.MyDate);
göz atın eylem örneğini görmek için aşağıdaki jsFiddle: şu şekildedir:
AJAX yukarıdaki yöntemleri kullanmak olsun ya da olmasın, FixDate yöntemi kullanabilirsiniz
http://jsfiddle.net/TroyAlford/TBNVV/
Not: Bu, AJAX bitlerini de içerir - ancak örnekte kullanılmazlar - yalnızca tamlık için orada bulunurlar.
Dojo Toolkit Tarih/Saat-idare etmenin yanı sıra dolu güzel bir sınıf ile birlikte gelir; [) harici bir kütüphane istemiyorum ki nedeni]
Yanıt için teşekkürler, ancak en uygun görünmüyor – marcgg
FYI, yazının yazarı https://bitbucket.org/pellepim/jstimezonedetect kullanılmasını önerir. – Denis