2016-07-18 191 views
0

我有這樣的字符串。我們如何以正確的格式顯示日期?現在它顯示如下:「測試日期」:「/ Date(1468783153887)/」。我在一個jQuery函數分配字符串值:以正確格式顯示日期

[{"RowIndex":1,"ObjectId":600290,"SpaceId":null,"Xposition":null,"Yposition":null,"Angle":null,"DrawingId":18137,"BlockRefHandle":"3DE","ObjectClassId":9387,"SymbolId":null,"ScaleFactor":null,"SymbolColor":120,"IsBlockBased":true,"NumberWithClassName":"DR-1040318 (Doors)","ObjectNo":"DR-1040318","ObjectClassName":"Doors","Equipment":"Doors","Address":"123:7yt","NFPA Classification":null,"Health Classification":null,"Test Method":"Functional","Test Result":"Passed","Test Date":"\/Date(1468783153887)\/","Dev. Reading":null,"Dev. Reading Date":"\/Date(1468783153887)\/","Barcode":null,"Site":"4433-75006-00045","Building":"Building Test","Floor":"Floor Test","Status":"Assigned","Attachments":"None","sfde":null,"Attribute for test - not mand":null,"Attribute for test":null,"Test Attribute":null,"intsi":null,"uiiiiiii":null,"Note":null,"s667":null,"sg":null,"ui":null,"My Attribute":null,"Comment":null,"Comments":null,"Location":null,"test12":null,"abcd":null,"Activate1":null,"Activating":null,"Activating Device Address":null,"Activating Device Location":null,"Shutdown Device Type":null,"Test2":null,"vvvd":null}] 

功能

function toolTipFulldata(i, columnAlias) {// building contents to tooltip 

    var html = '';//'<table border="1">'; 
    $.each(dataForToolTip[i], function (key, value) { 
      for (var j = 0; j < columnAlias.length; j++) { 
      if (key == columnAlias[j]) { 
       if (value == null)      
        value = "";          
       html += '<span>' + key + '</span>'; 
       html += '<span style="color:blue;">' + ":" + value + '</span></br>'; 
      } 
     } 
       }); 

    $("#showTooltip").html(html); 
};  

我們可以做任何事情來獲得適當的格式日期在該字符串?

+0

首先,確定日期是什麼格式,那麼你想要的格式它。顯示 –

+0

見http://stackoverflow.com/questions/4673527/ jquery-js –

回答

1

假設這些是使用Javascript的getTime毫秒:

var time = 1468783153887; 
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) 
    .AddMilliseconds(time) 
    .ToLocalTime(); 

然後,可變日期時間看起來像這樣: 17/07/2016 21點19分十三秒

解析JSON作爲數組,做這樣的:

var jsonString = @"[{""RowIndex"":1,""ObjectId"":600290,""SpaceId"":null,""Xposition"":null,""Yposition"":null,""Angle"":null,""DrawingId"":18137,""BlockRefHandle"":""3DE"",""ObjectClassId"":9387,""SymbolId"":null,""ScaleFactor"":null,""SymbolColor"":120,""IsBlockBased"":true,""NumberWithClassName"":""DR-1040318 (Doors)"",""ObjectNo"":""DR-1040318"",""ObjectClassName"":""Doors"",""Equipment"":""Doors"",""Address"":""123:7yt"",""NFPA Classification"":null,""Health Classification"":null,""Test Method"":""Functional"",""Test Result"":""Passed"",""Test Date"":""\/Date(1468783153887)\/"",""Dev. Reading"":null,""Dev. Reading Date"":""\/Date(1468783153887)\/"",""Barcode"":null,""Site"":""4433-75006-00045"",""Building"":""Building Test"",""Floor"":""Floor Test"",""Status"":""Assigned"",""Attachments"":""None"",""sfde"":null,""Attribute for test - not mand"":null,""Attribute for test"":null,""Test Attribute"":null,""intsi"":null,""uiiiiiii"":null,""Note"":null,""s667"":null,""sg"":null,""ui"":null,""My Attribute"":null,""Comment"":null,""Comments"":null,""Location"":null,""test12"":null,""abcd"":null,""Activate1"":null,""Activating"":null,""Activating Device Address"":null,""Activating Device Location"":null,""Shutdown Device Type"":null,""Test2"":null,""vvvd"":null}]"; 

var j = JArray.Parse(jsonString); 
var date = j[0]["Test Date"]; 
Console.WriteLine(date); 

它輸出這樣的:17/07/2016 19時19分13秒

+0

我有一個JSon字符串...那麼我們如何才能確定特定的項目是日期?如果我們將其確定爲日期,那麼我們可以繼續。但整個項目是一個字符串... :( –

+0

修改了原來的答案:) –

0

使用此轉換

var returned_date_string = "/Date(1468783153887)/"; 
 
var milli_seconds = /\d+/.exec(returned_date_string)[0]; 
 
console.log(milli_seconds); 
 
var returned_date = new Date(+milli_seconds); 
 
console.log(returned_date);