简体   繁体   English

使用以下格式转换datetime字符串:(yyyy-MM-dd'T'hh:mm:ss-zzz)

[英]Convert datetime string with this format: (yyyy-MM-dd'T'hh:mm:ss-zzz)

I am receiving a JSON string that contains a date that looks like this: 2015-07-09T08:38:49-07:00 where the last part is the timezone. 我收到的JSON字符串包含如下所示的日期: 2015-07-09T08:38:49-07:00其中最后一部分是时区。 Is there a standard way to convert this to a DateTimeOffset ? 有没有一种标准的方法将其转换为DateTimeOffset

Here is what I have so far: 这是我到目前为止:

var olu = JsonConvert.DeserializeObject<OneLoginUser>(jToken.ToString(), new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd'T'HH:mm:sszzz" });

This doesn't deserialize any of the dates. 这不会反转任何日期。 I've tried using -Z and hh:mm for the timezone data, but I can't seem to deserialize any of the dates. 我尝试使用-Zhh:mm作为时区数据,但我似乎无法反序列化任何日期。

For reference, this is from OneLogin, a SSO provider. 作为参考,这是来自一个SSO提供商OneLogin。 Here's a link to the user documentation. 这是用户文档的链接 Notice the bit about the dates at the top. 注意关于顶部日期的位。

That is a standard ISO 8601 extended format timestamp with offset, also covered by RFC 3339 . 这是带有偏移的标准ISO 8601扩展格式时间戳,也包含在RFC 3339中 There's nothing special about it. 没什么特别的。

DateTimeOffset.Parse("2015-07-09T08:38:49-07:00")

Or 要么

DateTimeOffset.ParseExact("2015-07-09T08:38:49-07:00", "yyyy-MM-dd'T'HH:mm:sszzz",
                                                       CultureInfo.InvariantCulture)

With JSON.Net, the defaults should work well. 使用JSON.Net,默认值应该可以正常工作。 No need to specify anything special. 无需指定任何特殊内容。

JsonConvert.DeserializeObject<DateTimeOffset>("\"2015-07-09T08:38:49-07:00\"")

The fiddle Brian posted in the question comments shows that it works when deserializing a larger object. 在问题评论中发布小提琴布莱恩表示,它可以在反序列化更大的对象时起作用。 If you're still not getting it to work, perhaps you could edit your question to show the specific JSON you're trying to deserialize and the object structure you're putting it into. 如果您还没有使用它,也许您可​​以编辑您的问题以显示您尝试反序列化的特定JSON以及您将其放入的对象结构。

One thing I noticed about your code, you show the json coming from jToken.ToString() , so somewhere you must have previously parsed using JObject.Parse . 有一点我注意到你的代码,你显示json来自jToken.ToString() ,所以你必须先使用JObject.Parse进行解析。 It's a little strange to do that, just to convert back to json and deserialize. 这样做有点奇怪,只是转换回json并反序列化。 Either go directly from the json string to the entity using JsonConvert.DeserializeObject , or use jToken.ToObject<OneLoginUser>() if you're already starting with jToken for some other reason. 使用JsonConvert.DeserializeObject直接从json字符串转到实体,或者如果由于其他原因已经使用jToken启动,则使用jToken.ToObject<OneLoginUser>() No need to mix both APIs, and it's possible you're loosing the date/time information in the process depending on what your settings are. 无需混合使用这两种API,您可能会在此过程中丢失日期/时间信息,具体取决于您的设置。

Try a format string like this: 尝试这样的格式字符串:

"yyyy-MM-dd'T'hh:mm:ss%K"

As you can see from the example, this parses better than what you specified (the duplicate hh:mm is probably screwing things up). 从示例中可以看出,这比您指定的更好解析(重复的hh:mm可能会搞砸了)。

string input = "2015-07-09T08:38:49-07:00";
DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd'T'hh:mm:ss%K", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(input);
Console.WriteLine(dt.ToString());

暂无
暂无

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

相关问题 如何通过linq将MM / dd / YYYY hh:mm:ss AM转换为YYYY-MM-dd datetime格式? - how to convert MM/dd/YYYY hh:mm:ss AM to YYYY-MM-dd datetime format by linq? 将字符串转换为应为“ yyyy-MM-dd hh:mm:ss”格式的SQL Server DateTime - Convert string to SQL Server DateTime which should be in “yyyy-MM-dd hh:mm:ss” format 尝试以(yyyy-mm-dd hh:mm:ss)日期格式转换字符串格式(dd-mm-yyyy hh:mm:ss) - Trying to convert string format (dd-mm-yyyy hh:mm:ss) in (yyyy-mm-dd hh:mm:ss) date format 将字符串转换为日期时间,格式为 yyyy-MM-dd HH:mm:ss in C# - convert string to datetime with form yyyy-MM-dd HH:mm:ss in C# 将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt - Convert string to Datetime dd/MM/yyyy hh:mm:ss tt 使用模式“MM / dd / yyyy hh:mm:ss”将String转换为DateTime - Convert String to DateTime with pattern “MM/dd/yyyy hh:mm:ss” 将日期时间从YYYY-MM-DDThh:mm:ss转换为YYYY-MM-DD hh:mm:ss格式 - Converting datetime from YYYY-MM-DDThh:mm:ss to YYYY-MM-DD hh:mm:ss format 将字符串dd / mm / yyy转换为yyyy-mm-dd hh:mm:ss - Convert string dd/mm/yyy to yyyy-mm-dd hh:mm:ss 为什么“YYYY-MM-DD HH”:&#39;MM&#39;:&#39;SS“日期时间格式显示不正确? - Why is “YYYY-MM-DD HH':'MM':'SS” DateTime Format Displaying Incorrectly? DateTime.ParseExact对“ yyyy-MM-dd HH:mm:ss”不起作用 - DateTime.ParseExact doesn't work for “yyyy-MM-dd HH:mm:ss”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM