DateTime and Daylight Saving Time in Dart

Thomas Middel
Pinch.nl
Published in
3 min readNov 23, 2021

--

Handling dates in Flutter is easy, perhaps a bit too easy? We can easily use and modify dates and times, but this can get you into trouble sooner than you might expect. The title might give it away, but daylight saving time is a typical case of what could cause some serious issues.

Modifying dates

First things first: how can we actually handle dates and times in Flutter/Dart? We can instantiate a DateTime object by setting its parameters (think year, month, day, etc) or by parsing it (for example from a String or a timestamp). If you need the current date and time, simply use DateTime.now().

Easy peasy!

If you set the DateTime through the parameters and use values greater than they can be (f.e. over 12 months, or more than 30/31 days) DateTime will automatically do the calculation for you! This means that if we try to set DateTime(2021, 13, 22) it’ll automatically set the object to the first month of 2022.

Now that we have our DateTime object, we can start to modify it. What if we need this exact…

--

--