Jaimin Modi
Jaimin Modi

Reputation: 1677

Converting GMT date to local time zone date - flutter

I am getting GMT date from api response:

Thu, 13 Oct 2022 00:09:35 GMT

Using below code to convert it to local date :

DateTime dt = DateTime.parse("Thu, 13 Oct 2022 00:09:35 GMT");
print("converted gmt date >> " + dt.toString());
final _localTime = dt.toLocal();
print("local modified date >> "+_localTime.toString());

But Getting below error :

Unhandled Exception: FormatException: Invalid date format E/flutter ( 3323): Thu, 13 Oct 2022 00:09:35 GMT E/flutter ( 3323): #0 DateTime.parse (dart:core/date_time.dart:347:7) E/flutter ( 3323): #1 HomeProvider.fetchModifiedDate. (package:mahotsav/src/framework/dataProvider/Home/homeProvider.dart:1677:32) E/flutter ( 3323): #2 _rootRunUnary (dart:async/zone.dart:1434:47) E/flutter ( 3323): #3 _CustomZone.runUnary (dart:async/zone.dart:1335:19) E/flutter ( 3323):

What might be the issue?

Upvotes: 0

Views: 853

Answers (1)

Emre Faruk KOLAÇ
Emre Faruk KOLAÇ

Reputation: 532

DateTime expects time in YYYY-MM-DD HH:MM:SS format. If you change your format before parsing, it should convert it successfully.

Upvotes: 0

Related Questions