diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index 64ab2efc3..26870efb3 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -845,6 +845,18 @@ public void Can_Deserialize_Dictionary_with_Null() Assert.IsNull(dictionary["Null"]); } + [Test] + public void Can_Deserialize_Date_With_Milliseconds() + { + const string content = "{ \"CreatedOn\": \"2018-10-01T14:39:00.234Z\" }"; + JsonDeserializer json = new JsonDeserializer(); + DateTimeResponse output = json.Deserialize(new RestResponse { Content = content }); + DateTime expected = DateTime.Parse("2018-10-01 14:39:00.234", CultureInfo.InvariantCulture); + + Assert.NotNull(output); + Assert.AreEqual(expected.ToString(), output.CreatedOn.ToString()); + } + private static string CreateJsonWithUnderscores() { JsonObject doc = new JsonObject(); diff --git a/RestSharp.Tests/SampleClasses/misc.cs b/RestSharp.Tests/SampleClasses/misc.cs index a32ffa21b..748d4adc4 100644 --- a/RestSharp.Tests/SampleClasses/misc.cs +++ b/RestSharp.Tests/SampleClasses/misc.cs @@ -333,4 +333,9 @@ public class WrongNote [DeserializeAs(Content = true)] public string Text { get; set; } } + + public class DateTimeResponse + { + public DateTime CreatedOn { get; set; } + } } diff --git a/RestSharp/Extensions/StringExtensions.cs b/RestSharp/Extensions/StringExtensions.cs index e218f031d..e789bf2a9 100644 --- a/RestSharp/Extensions/StringExtensions.cs +++ b/RestSharp/Extensions/StringExtensions.cs @@ -143,13 +143,14 @@ private static DateTime ParseFormattedDate(string input, CultureInfo culture) "yyyy-MM-dd HH:mm:ssZ", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-ddTHH:mm:sszzzzzz", + "yyyy-MM-ddTHH:mm:ss.fffZ", "M/d/yyyy h:mm:ss tt" // default format for invariant culture }; if (DateTime.TryParseExact(input, formats, culture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var date)) return date; - + return DateTime.TryParse(input, culture, DateTimeStyles.None, out date) ? date : default(DateTime); }