標籤:返回 字元 資料 datetime date eve 服務 string return
以前做項目一直遇到帶資料庫裡面帶datetime類型資料 返回到前台會變成時間戳記
這顯然不是我們想要的
於是我在最初的項目中 選擇在前端JS格式化date(xxxx)形式的方式處理 現在想來有點蠢
今天靈機一動 想到是不是可以在服務端就把datetime轉成string返回到 結果就有了此文
服務端:
1 DataGetEntities context = new DataGetEntities(); 2 var queryTyphoon = context.T_TyphoonPaths.Where(i => i.typhooncode == typhoonCode).ToList(); 3 Newtonsoft.Json.Converters.IsoDateTimeConverter iso = new Newtonsoft.Json.Converters.IsoDateTimeConverter(); 4 iso.DateTimeFormat = "yyyy-MM-dd hh:mm:ss"; 5 6 var path = (from p in queryTyphoon 7 select new 8 { 9 p.id,10 p.air_pressure,11 p.dottype,12 p.latitude,13 p.longitude,14 p.move_direction,15 p.move_speed,16 p.seven_wind,17 p.ten_wind,18 p.typhooncode,19 p.typhoonname,20 p.wind_power,21 p.wind_speed,22 p.wind_time23 });24 return Content(JsonConvert.SerializeObject(path, iso));
這裡需要注意的 此時在前端返回的JSON其實是字串並不是對象所以在前端 需要轉一下:
1 var tData = JSON.parse(data);
C# 使用linq處理返回帶datetime類型資料 json顯示/date(xxxxx)/