我将2012-12-11下午1点(我在美国东部时间安大略省)插入网络表单并将其发布到我的本地网络服务器,并将其转换为 UTC time(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
});
}
答案 0 :(得分:0)
我最终使用以下内容在服务器上转换UTC并将日期(作为字符串)返回到客户端应用程序,并使用以下内容...
Public Shared Function GetJson(ByVal dt As DataTable) As String
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)
For Each col As DataColumn In dt.Columns
If col.ColumnName = "Start_Date" Or col.ColumnName = "End_Date" Then
'insert default date
Dim convertedDate As String = TimeZoneInfo.ConvertTimeFromUtc(dr(col), TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))
row.Add(col.ColumnName, convertedDate)
Else
row.Add(col.ColumnName, dr(col))
End If
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Function
答案 1 :(得分:0)
也许更简单的方法是在https://github.com/GregDThomas/jquery-localtime使用插件 - 它只需要服务器传递给客户端的UTC字符串,然后使用本地时区的客户端知识将其呈现给用户。