sở thích của tôi là để lưu trữ tất cả các ngày trên phía máy chủ sử dụng thời gian UTC, và khi tôi đang xử lý dữ liệu trở lại thông qua AJAX gọi, để tạo ra một handler toàn cầu mà hiện một số phân tích cú pháp.
Ví dụ sau đây cho phép bạn chỉ cần sử dụng:
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...
}
});
Tuy nhiên, nó pre-phân tích bất kỳ giá trị trả lại trong phần "dữ liệu" là "thành công" chức năng của bằng cách sửa chữa tru để kiểm tra thời gian tính theo giờ UTC để múi giờ địa phương. Hãy lưu ý - sau khi thực hiện việc này, nếu bạn xử lý thêm dữ liệu, bạn sẽ cần phải sửa UN trước khi gửi lại máy chủ, hoặc bạn sẽ đăng nó trở lại với bù đắp.
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ất cả thiết lập để thực hiện việc này. Nếu bây giờ bạn xử lý những thứ như ngày tháng trong OnPostSuccess, bạn có thể đảm bảo rằng chúng luôn ở định dạng đúng - và luôn ở đúng múi giờ.
hay không bạn sử dụng trên AJAX phương pháp, bạn có thể sử dụng FixDate phương pháp như sau:
var MyObj = {
MyDate: "\/Date(12345678)\/"
};
console.log('Before: ', MyObj.MyDate);
MyObj = app.FixDate(MyObj);
console.log('After: ', MyObj.MyDate);
Để xem ví dụ trong hành động, hãy kiểm tra jsFiddle sau:
http://jsfiddle.net/TroyAlford/TBNVV/
Lưu ý: điều này cũng bao gồm các bit AJAX - nhưng chúng không được sử dụng trong ví dụ - chỉ ở đó để hoàn thành.
Cảm ơn câu trả lời, nhưng dường như không tối ưu – marcgg
FYI, tác giả của bài đăng đề xuất sử dụng https://bitbucket.org/pellepim/jstimezonedetect để thay thế – Denis