2017-04-03 28 views
-2

以下是我的DTO。爲什麼在DateTime屬性中添加時區

public class CustomerTO 
{ 
    public int Id { get; set;} 
    public DateTime? RDate {get;set;} 
    public DateTime? LDate {get; set;}  

} 

當我在數據庫檢查是相應的列RDATE & LDate值如下: -

RDate = 2017-04-03 21:09:24.273 
LDate = 2017-04-03 21:09:24.577 

我的行動。

public string Get() 
{ 
    var customers = dal.GetRecords(); 
    //if I hover the customers object & see the RDate or LDate value it is as desired. 

var strJson= JsonConvert.SerializeObject(customers); 
//but here the value of RDate/LDate has Time zone appended. 
} 

strJson包含RDate & LDate值如下。

RDate = 2017-04-03T21:09:24.5768697+05:30 
LDate = 2017-04-03T21:09:24.5618688+05:30 // why +5:30 timezone is getting appended. 

如何解決?

任何幫助/建議高度讚賞。

+0

它的存在,所以如果JSON是另一個時區反序列化,原來的日期/時間將被保留。 – stuartd

+0

@stuartd,對不起,我沒有找到你 –

回答

0

嘗試設置JsonSerializerSettingsUnspecifiedDateTimeZoneHandling屬性:

var strJson = JsonConvert.SerializeObject(customers, new JsonSerializerSettings 
{ 
    DateTimeZoneHandling = DateTimeZoneHandling.Unspecified 
}); 
+0

但是在我的項目中還有其他地方沒有這樣的問題。請添加可能導致此問題的內容。 謝謝 –

+0

您將需要提供一個回購:https://stackoverflow.com/help/mcve。請參閱此:http://stackoverflow.com/questions/10033612/prevent-json-net-4-5-from-appending-timezone-offset-when-using-microsoftdateform – mm8

相關問題