'Not working while converting IST to EST in Flutter

DateTime myDT = DateTime.now();//Current DateTime  
print("current time :- ${myDT}"); 

//current time :- 2022-03-24 17:44:12.158026

DateTime EST = dateTimeToZone(zone: "EST", datetime: myDT);//DateTime in EST zone  
print("EST time :- ${EST}");  

//EST time :- 2022-03-24 07:14:12.158026Z

this is my code. And right now I check EST zone time on Google it's 8:14 . Why I m getting difference? I doing this with instant 0.4.1 plug-in.



Solution 1:[1]

Normally, I would just suggest to not use "EST", as it is fixed to UTC-5, but rather to use an IANA time zone identifier for the time zone you are asking about. For example, in the US, the Eastern time zone is represented by "America/New_York". In Canada, you would use "America/Toronto". These presently use UTC-5 during standard time, and UTC-4 during daylight time.

Unfortunately, the Dart/Flutter dateTimeToZone function has no awareness of IANA time zones. It thus does not account for daylight saving time or historical changes to offsets that are part of the nature of time zones. Instead, it uses a fixed (and incomplete) mapping of abbreviations to offsets, defined in timeZoneOffsets.

To overcome this limitation, you can try using the timezone package, which provides support for IANA time zones. Then you can use "America/New_York" for the US Eastern time zone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1