0

Here is my Dart code,

void main() {
  DateTime dt1 = DateTime.tryParse("2018-11-23T12:32:53.2928904");
  DateTime dt2 = DateTime.tryParse("2018-11-23T12:32:53.292890");
  print(dt1);  
  print(dt2);
}

with result,

null
2018-11-23 12:32:53.293

I'm getting a json result from a rest API which contains the DateTime value

"2018-11-23T12:32:53.2928904"

But when I try to parse it, I'm getting null. When i reduce one character at the end I'm able to parse it. How do I parse the result to a DateTime object? The rest API is developed with ASP.net Framework 4.5 in c#.

Nate Bosch
  • 10,145
  • 2
  • 26
  • 22
Vishnu
  • 1,757
  • 2
  • 11
  • 19
  • Look like the format is `yyyy-MM-ddTHH:mm:ss.fffffff`, a parse exact will do . But does one even need a precision of the ten millionths of a second in a datetime value? https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=netframework-4.7.2 – xdtTransform Nov 23 '18 at 07:29

1 Answers1

2

Since the dart DateTime.tryParse method doesn't allow you specify a format, the best ways to make ASP.NET and dart interoperate are probably:

  • Customize the JSON.NET output of the API to only include 6 places of fractional seconds (see Specifying a custom DateTime format when serializing with Json.Net)
  • Parse dates with one of the dart wrappers for moment.js on the client
  • Just chop off the last digit using string manipulation - it is beyond the precision of JS date objects anyway
Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46