我将 2012 年 12 月 11 日下午 1 点(我在安大略省,这是美国东部标准时间)插入 Web 表单并将其发布到我的本地 Web 服务器,然后将其转换为UTC时间(Asp.Net start_date = CDate(start_date).ToUniversalTime )之前我将它保存在数据库中。UTC 时间是数据库字段中的下午 6 点。我使用以下代码(见下文)将保存的 UTC 日期时间转换为用户,并愉快地显示下午 1 点(GMT-5:00)。
当我在加利福尼亚的生产服务器上发布下午 1 点的同一时间时,数据库中保存的时间是晚上 9 点。所以 GMT-8:00 应该在客户端的浏览器上给我下午 1 点。通过生产服务器显示的时间是晚上 7 点?为什么以及是否有解决办法?
我通过 asp.net json 从数据库返回值,然后使用 moment.js 将其转换为 UTC 数字,然后运行 utcToLocal 函数。如何显示预期的下午 1 点?
function utcToLocal(utc) {
// Create a local date from the UTC string
var t = new Date(Number(utc));
// Get the offset in ms
var offset = t.getTimezoneOffset() * 60000;
// Subtract from the UTC time to get local
t.setTime(t.getTime() - offset);
// do whatever
var d = [t.getFullYear(), t.getMonth(), t.getDate()].join('/');
d += ' ' + t.toLocaleTimeString();
return d;
}
//.format("YYYY-MM-DD h:mm a")
function Get_History(filter_date, msg) {
//div_history
var jsonText = JSON.stringify({
filter_date: filter_date, UserID: userid
});
$.ajax({
type: "POST",
url: "cc_m.aspx/getHistory",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d != "0") {
var obj = $.parseJSON(data.d);
$('.div_history').html('');
$.each(obj, function (index, value) {
$('.div_history').append(
"<div class='grid_history' style='border: solid 1px silver;'>" +
"Group: " + value.Group_Name + "<br/>" +
"Teacher: " + value.Teacher + "<br/>" +
"Child: " + value.Child_Name + "<br/>" +
"Category: " + value.Category + "<br/>" +
"Item: " + value.Item + "<br/>" +
"From: " + utcToLocal(moment.utc(value.Start_Date)) + "<br/>" +
"To: " + utcToLocal(moment.utc(value.End_Date)) + "<br/>" +
"Note: " + value.Other + "<br/>" +
"Status: " + value.Status + "<br/>" +
"<fieldset data-role='controlgroup' data-type='horizontal' data-mini='true' >" +
"<button class='send_item' rel='" + value.id_Group + "' data-icon='envelope' data-theme='b' >Send</button>" +
"<button class='edit_item' rel='" + value.id + "' data-icon='edit' data-theme='a' >Edit</button>" +
"<button class='reset_item' rel='" + value.id + "' data-icon='repeat' data-theme='a' >Reset</button>" +
"<a data-role='button' class='delete_item' rel='" + value.id + "' data-icon='remove' data-theme='a' href='#popup_delete' data-rel='dialog'>Delete</a><br/>" +
"</fieldset></div><br/>"
);
$(".div_history").trigger("create");
//on add history update msg at top of page to NEW RECORD! or ACTIVITY RECORDS SENT! or ACTIVITY RESET TO PENDING! and scroll to top
//if (msg.length > 0) {
$(".grid_msg").html(msg);
//}
});
}
} //end success
});
}