Get Last Month Date In Flutter / Dart

We can calculate both first day of the month and the last day of the month:

DateTime firstDayCurrentMonth = DateTime.utc(DateTime.now().year, DateTime.now().month, 1);

DateTime lastDayCurrentMonth = DateTime.utc(DateTime.now().year,DateTime.now().month+1,).subtract(Duration(days: 1));

DateTime.utc takes in integer values as parameters: int year, int month, int day and so on.


You can just use

var prevMonth = new DateTime(date.year, date.month - 1, date.day);

with

var date = new DateTime(2018, 1, 13);

you get

2017-12-13

It's usually a good idea to convert to UTC and then back to local date/time before doing date calculations to avoid issues with daylight saving and time zones.