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
}
Author by
Janaka
Updated on December 25, 2022Comments
-
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?
-
harism over 3 yearsLink from elsewhere on SO; stackoverflow.com/a/60188960/680519 Hope this helps (I am totally new to Flutter).
-
-
Arvind Kumar Avinash over 3 yearsI 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 writeprint(utc);
which will implicitly callprint(utc.toString());
-
Joy Terence over 3 yearsYou 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) :)