purplesoup
asked on
Converting Newtonsoft.Json.Linq.JToken to a DateTime
I've got some Twitter data that looks like this:
? jsonDat[x]["created_at"].T oString()
"Mon Oct 24 07:32:25 +0000 2016"
But I'm having trouble converting it to a C# DateTime.
Here are some things I've tried and the responses.
It could be because I'm in the UK and the DateTime format is different - any suggestions on how I can extract a DateTime out of this value?
? jsonDat[x]["created_at"].T
"Mon Oct 24 07:32:25 +0000 2016"
But I'm having trouble converting it to a C# DateTime.
Here are some things I've tried and the responses.
? DateTime.Parse("Mon Oct 24 07:32:25 +0000 2016")
'DateTime.Parse("Mon Oct 24 07:32:25 +0000 2016")' threw an exception of type 'System.FormatException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: "String was not recognized as a valid DateTime."
Source: "mscorlib"
StackTrace: " at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n at System.DateTime.Parse(String s)"
TargetSite: {System.DateTime Parse(System.String, System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles)}
--
? System.Convert.ChangeType(jsonDat[x]["created_at"].ToString(),typeof(DateTime))
'System.Convert.ChangeType(jsonDat[x]["created_at"].ToString(),typeof(DateTime))' threw an exception of type 'System.FormatException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: "String was not recognized as a valid DateTime."
Source: "mscorlib"
StackTrace: " at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n at System.Convert.ToDateTime(String value, IFormatProvider provider)\r\n at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)\r\n at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)\r\n at System.Convert.ChangeType(Object value, Type conversionType)"
TargetSite: {System.DateTime Parse(System.String, System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles)}
--
? jsonDat[x]["created_at"].ToObject<DateTime>();
'jsonDat[x]["created_at"].ToObject<DateTime>()' threw an exception of type 'System.FormatException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: "String was not recognized as a valid DateTime."
Source: "mscorlib"
StackTrace: " at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n at System.Convert.ToDateTime(String value, IFormatProvider provider)\r\n at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)\r\n at System.Convert.ToDateTime(Object value, IFormatProvider provider)\r\n at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)\r\n at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)\r\n at Newtonsoft.Json.Linq.JToken.ToObject[T]()"
TargetSite: {System.DateTime Parse(System.String, System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles)}
It could be because I'm in the UK and the DateTime format is different - any suggestions on how I can extract a DateTime out of this value?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to modify it - but you weren't to know it was 24 hour clock.
? DateTime.ParseExact("Mon Oct 24 17:02:54 +0000 2016","ddd MMM dd HH:mm:ss zzzz yyyy", Thread.CurrentThread.Curre ntCulture) ;
{24/10/2016 18:02:54}
Date: {24/10/2016 00:00:00}
Day: 24
DayOfWeek: Monday
DayOfYear: 298
Hour: 18
Kind: Local
Millisecond: 0
Minute: 2
Month: 10
Second: 54
Ticks: 636129289740000000
TimeOfDay: {18:02:54}
Year: 2016
? DateTime.ParseExact("Mon Oct 24 17:02:54 +0000 2016","ddd MMM dd HH:mm:ss zzzz yyyy", Thread.CurrentThread.Curre
{24/10/2016 18:02:54}
Date: {24/10/2016 00:00:00}
Day: 24
DayOfWeek: Monday
DayOfYear: 298
Hour: 18
Kind: Local
Millisecond: 0
Minute: 2
Month: 10
Second: 54
Ticks: 636129289740000000
TimeOfDay: {18:02:54}
Year: 2016
The lack of an AM/PM designator should be an indicator that this was needed in 24-hr format and I apologize for missing that. Otherwise, I am glad to have been of assistance.
-saige-
-saige-
ASKER