2

In my flutter application I get appointment time in UTC format from a server. I would like to display it in local time.

Is there a way to convert UTC to local time in flutter?

Janaka
  • 2,505
  • 4
  • 33
  • 57
  • Link from elsewhere on SO; https://stackoverflow.com/a/60188960/680519 Hope this helps (I am totally new to Flutter). – harism Nov 06 '20 at 17:06

1 Answers1

6

Dart has inbuilt DateTime type that provides quite a few handy methods to convert between time formats.

void main() {
  var utc = DateTime.parse("2020-06-11 17:47:35 Z");
  print(utc.toString());             // 2020-06-11 17:47:35.000Z
  print(utc.isUtc.toString());       // true
  print(utc.toLocal().toString());   //2020-06-11 23:17:35.000
}
Joy Terence
  • 686
  • 5
  • 11
  • I do not know `Dart` but I'm wondering if you really need to use `.toString()` in the print statement. In Java and similar languages, you can simply write `print(utc);` which will implicitly call `print(utc.toString());` – Arvind Kumar Avinash Nov 06 '20 at 21:04
  • You don't need it. Since I'd initially typed it out with Text widget on flutter dartpad, I had to use the `.toString()` and I kept it as it is in the example posted too. But you can just as well do print(UTC) :) – Joy Terence Nov 07 '20 at 16:33