简体   繁体   English

json字符串中的日期时间将转换为本地时间

[英]Datetime in json string is getting converted to localtime

Why is the datetime+offset is getting converted to localtime+offset when convert the json string to Jobject. 将json字符串转换为Jobject时,为什么datetime + offset被转换为localtime + offset。

Here is the code. 这是代码。

string dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";
JObject json = JObject.Parse(dataValue);
Console.Write(json.ToString());

Output: 输出:

{
  "Time": "2016-07-15T17:33:41+05:30"
}

Expected Output: 预期产量:

{
  "Time": "2016-07-15T20:03:41+08:00"
}

By default it will be using the local time, but you can override the default settings: 默认情况下,它将使用当地时间,但是您可以覆盖默认设置:

var dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";

var jsonSerializerSettings = new JsonSerializerSettings
{
    DateTimeZoneHandling = DateTimeZoneHandling.Utc
};

var json = JsonConvert.DeserializeObject<JObject>(dataValue, jsonSerializerSettings);

If you don't care about the date being converted to a DateTime type, you can tell Json.NET to just ignore dates and parse the value as a string : 如果您不关心将日期转换为DateTime类型,可以告诉Json.NET忽略日期并将值解析为字符串

var dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";

var jsonSerializerSettings = new JsonSerializerSettings
{
    DateParseHandling = DateParseHandling.None
};

var json = JsonConvert.DeserializeObject<JObject>(dataValue, jsonSerializerSettings)

In such a way it will stay exactly as your input. 这样,它将完全保持您的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串未转换为日期时间 - String not getting converted to datetime 为什么字符串不能转换为日期时间? - Why string cannot be converted to dateTime? 日期时间到字符串:取决于转换的日期时间的不同行为 - Datetime to string: differents behavior depending on the converted datetime 从JavaScript客户端发送到.NET的字符串格式的UTC日期已转换为DateTime - UTC date in string format send from JavaScript client to .NET is getting converted into DateTime 为什么不将该字符串转换为DateTime? - Why won't this string be converted to DateTime? 将转换后的String排序为DateTime列。 - Sorting a converted String to DateTime column. 获取 JSON 值的 POST 请求无法转换为 System.String。 路径错误 - POST request getting JSON value could not be converted to System.String. Path error 我的 Enum 正在转换为空字符串,而 struct 正在转换为空字典 - My Enum is getting converted to an empty string and struct converted to an empty dictionary 如何使用EF Core 2.2将JSON_VALUE转换为DateTime? - How can a JSON_VALUE be converted to a DateTime with EF Core 2.2? JSON 反序列化 - 字符串自动转换为 Int - JSON Deserialization - String Is Automatically Converted To Int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM