How to display UTC time in local time

1,468

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
}
Share:
1,468
Janaka
Author by

Janaka

Updated on December 25, 2022

Comments

  • Janaka
    Janaka over 1 year

    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?

  • Arvind Kumar Avinash
    Arvind Kumar Avinash over 3 years
    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());
  • Joy Terence
    Joy Terence over 3 years
    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) :)