C# 使用linq處理返回帶datetime類型資料 json顯示/date(xxxxx)/

來源:互聯網
上載者:User

標籤:返回   字元   資料   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)/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.