2017-10-08 18 views
0

我正在查詢我的數據庫並返回一個jsonresult,並使用它來填充網格,並且我看到返回的日期是/ Date(1507477743793)/。我環顧四周,當它涉及到轉換一個變量,但不是當涉及到對象的數組(我認爲是正確的)如何將Json日期從/Date.../轉換爲從jsonresult返回時的實際日期時間?

編輯

的樣本數據已經看到了解決方案

FileAReportID:1 
ReportAddress:"1 Main Street West" 
ReportDate:"/Date(1507477743793)/" 
ReporterName:"Beetle Bailey" 
+0

只爲你正在使用JSON response.I意味着.NET,JAVA等,這技術澄清.. –

+0

對此深感抱歉,我使用Ajax來獲取返回的JSON – Chris

+0

在其技術後臺寫 –

回答

0

這個號碼是1507477743793是你的服務器的時間戳。在其他格式CONVER這只是試試這個:

let t = (new Date(1507477743793)).toUTCString(); 
console.log(t) 

// output 
and you get: 2017/12/6 05:32:30pm 
0

可以創建自定義jsonresult

public class CustomJsonResult : JsonResult 
{ 
    private const string _dateFormat = "yyyy-MM-dd HH:mm:ss"; 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
     { 
      throw new ArgumentNullException("context"); 
     } 

     HttpResponseBase response = context.HttpContext.Response; 

     if (!String.IsNullOrEmpty(ContentType)) 
     { 
      response.ContentType = ContentType; 
     } 
     else 
     { 
      response.ContentType = "application/json"; 
     } 
     if (ContentEncoding != null) 
     { 
      response.ContentEncoding = ContentEncoding; 
     } 
     if (Data != null) 
     { 
      // Using Json.NET serializer 
      var isoConvert = new IsoDateTimeConverter(); 
      isoConvert.DateTimeFormat = _dateFormat; 
      response.Write(JsonConvert.SerializeObject(Data, isoConvert)); 
     } 
    } 
} 

用法示例:

[HttpGet] 
public ActionResult Index() { 
    return new CustomJsonResult { Data = new { } }; 
} 

您還可以創建自定義MediaTypeFormatter格式化和序列化過程中你可以改變日期時間的格式。

更新

Json.Net支持格式的日期,如果你從一個Ajax消耗它叫你可能想看看JavaScriptDateTimeConverter的多種方式。

Serializing Dates in JSON

+0

我看到一個這樣的例子,但我有點希望在jQuery中做到這一點,當數據從ajax調用成功返回 – Chris

相關問題