Home > OS >  Flutter rounds DateTime value to 3 decimal places
Flutter rounds DateTime value to 3 decimal places

Time:01-26

I'm developing a web app using Flutter front-end and FastApi. I encountered this issue while using DateTime, that its value has a different length in either of the languages (Python & Dart).

DateTime object from python

2022-01-25T13:09:03.914910

DateTime object parsed from response

2022-01-25 13:09:03.915

I observed that Dart rounds the DateTime value to 3 decimal places.

How to get around this issue.

CodePudding user response:

You're probably using the default toString() on the DateTime object. What you might need is the toIso8601String() method. Here's a code sample to show the difference.

final now = DateTime.now();
print(now); // 2022-01-25 11:04:08.053
print(now.toIso8601String()); // 2022-01-25T11:04:08.053

CodePudding user response:

Dart for the web is transpiled to JavaScript and is subject to limitations of JavaScript and of the browser. JavaScript Date objects do not support microseconds, so Dart's DateTime will not either in that environment. DateTime.toString() using the Dart VM/runtime will include non-zero microseconds.

See https://github.com/dart-lang/sdk/issues/44876.

(Additionally, note that modern web browsers typically reduce precision when measuring time in an attempt to mitigate timing attacks, although that's separate from parsing timestamps from strings.)

  •  Tags:  
  • Related