2011-06-01 15 views
0

我有一個使用EF 4.1的ASP.NET MVC 3應用程序。我從包含一些DateTime信息的數據庫中提取一些數據並將其顯示在jqGrid中。通常情況下,顯示的日期是確定的,但某些人在我的jqGrid上顯示時會關閉一小時,我相信這可能是由於夏季時間應用造成的。DateTime顯示是關閉一小時......有時

例如,檢索到的數據包括日期,像這樣:

PromoStartDate = {10/31/1987 11:14:13 AM} 

但被顯示的頁面(jqGrid newformat= 'G:i m/d/Y')上的是:

12:14 10/31/1987 

其是關閉一小時。展望螢火蟲,我看到,在響應PromoStartDate是::

"PromoStartDate":"\/Date(562695253060)\/" 

通過的jsfiddle運行這個(http://jsfiddle.net/u9yMM/2/)我得到:

Sat Oct 31 1987 12:15:13 GMT-0400 (Eastern Daylight Time) 

與我的機器的時區打圍在瀏覽器中調整時間(例如,設置爲大西洋時間可以將所有內容調整爲+1小時),但上述日期仍然不正確(意味着它仍然是1小時)。

JsonResult具有我期望的(即時間值爲該條目的11:14:13),所以我現在對此有點困惑。

想法?

+0

你想顯示本地時間的日期? toString()似乎將其轉換爲本地時間。 – 2011-06-01 18:04:20

+0

@ Can Gencer - 是的,當地時間是我需要展示的。 – itsmatt 2011-06-01 18:09:56

+0

發佈的日期似乎是正確的,即562695253060點到星期六,1987年10月31日16:15:13 GMT,這將在GMT -4東部日光下12:15。我的猜測是,序列化層出了問題... – 2011-06-01 18:36:12

回答

0

我做了一些玩弄這個..

var date = new DateTime(1987, 10, 31, 11, 14, 13, DateTimeKind.Utc);    
    return Json(new {Date = date}, JsonRequestBehavior.AllowGet); 

這將返回{"Date":"\/Date(562677253000)\/"}這似乎是正確的。在JavaScript中, new Date(562677253000).toUTCString()評估爲「星期六,1987年10月31日11:14:13 GMT」。

所以在我看來,你的日期從數據庫中存儲或讀取的方式有問題。

new Date(..)計算日期基於1970年1月1日00:00:00 UTC,我會想象在ASP.NET中內置的JSON序列化器在序列化它們時也是如此。

1

這是我在測試MVC3時發現的(不是真的答案)。如果要在美國更改DST時序列化的DateTime是2007年以前,那麼在處理DST更改的洞中處理日期時間時,服務器上發生的任何日期序列化將爲1小時。 (http://en.wikipedia.org/wiki/DST_in_the_US)。基本上,它似乎在所有日期都使用最新的DST規則。

例子。

Server Time: Friday, March 12, 2004 10:15:00 AM 
JSON Serialization: /Date(1079115300000)/ 
JS Time Formated: 10:15 

Saturday, March 13, 2004 10:15:00 AM 
JSON Serialization: /Date(1079201700000)/ 
JS Time Formated: 10:15 

Sunday, March 14, 2004 10:15:00 AM 
JSON Serialization: /Date(1079288100000)/ 
JS Time Formated: 11:15 (failed used the post 2007 DST rules) 

Server Time: Monday, March 15, 2004 10:15:00 AM 
JSON Serialization: /Date(1079374500000)/ 
JS Time Formated: 11:15 (failed used the post 2007 DST rules) 

最後兩項無法正確序列化數據。

在此測試案例中,服務器和客戶端託管在同一臺計算機上,並且已應用所有修補程序。從服務器

public ActionResult GetDates() 
    { 
     return Json(GetTimeList(), 
      "text/x-json", 
      System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet); 

    } 

    private List<TestModel> GetTimeList() 
    { 
     List<TestModel> model = new List<TestModel>(); 

     DateTime temp = new DateTime(2003, 1, 1, 10, 15, 0); 
     int HourInc = 24; 
     for (int i = 0; i < 2200; i ++) 
     { 
      model.Add(new TestModel { ServerDate = temp.ToLongDateString() + " " + temp.ToLongTimeString(), ServerTime = temp, ServerTimeString = temp.ToString("HH:mm") }); 
      temp = temp.AddHours(HourInc); 
     } 

     return model; 
    } 

代碼從客戶端

代碼片段

<script type="text/javascript"> 

    $(function() { 

     $.getJSON('/test/GetDates', function (data) { 
      var newhtml = ''; 
      var s = '<td>'; 
      var e = '</td>'; 
      newhtml = "<tr><th>ServerDate</th><th>ServerTime</th><th>JsonDate</th><th>JsaonFormatedTime</th></tr>"; 
      $.each(data, function() { 
       var formatedTime = formatDateTime(parseJSON(this.ServerTime)) 
       var st = formatedTime == this.ServerTimeString ? "pass" : "fail"; 
       newhtml += '<tr class="' + st + '">'; 
       newhtml += s + this.ServerDate + e; 
       newhtml += s + this.ServerTimeString + e; 
       newhtml += s + this.ServerTime + e; 
       newhtml += s + formatedTime + e; 
       newhtml + '</tr>'; 

      }) 

      $('#test').html(newhtml) 
     }); 

    }); 

    var reDateNet = /\/Date\((\-?\d+)\)\//i; 
    function parseJSON (value) { 
     if (value == '/Date(-62135568000000)/') return null; // .net min date 
     else if (reDateNet.test(value)) { 
      return new Date(parseInt(reDateNet.exec(value)[1], 10)); 
     } 
     return value; 
    } 

    function formatDateTime(dt) { 
     var s = '', d = dt.getHours(); 
     s += (d < 10 ? '0' + d : d) + ':'; 
     d = dt.getMinutes(); 
     s += (d < 10 ? '0' + d : d); 
     return s; 
    } 

</script>