0

2012-12-11 1pm(私はESTであるオンタリオにいます)を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
            });

        }
4

2 に答える 2

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
于 2012-12-12T00:03:39.787 に答える
0

おそらく、より簡単な方法は、https://github.com/GregDThomas/jquery-localtimeでプラグインを使用することです。サーバーがクライアントに渡す UTC 文字列を取得し、ローカル タイムゾーンに関するクライアントの知識を使用して表示します。それをユーザーに。

于 2013-02-12T09:52:34.967 に答える