getDateTimePreviousMonth function

DateTime getDateTimePreviousMonth(
  1. int month, {
  2. int? year,
})

Returns start of previous month, using as reference month and year.

month from 1 to 12. year if null uses year from DateTime.now .

Implementation

DateTime getDateTimePreviousMonth(int month, {int? year}) {
  year ??= DateTime.now().year;
  var cursor = DateTime(year, month, 1, 0, 0, 0, 0, 0);
  var prev = cursor.subtract(Duration(days: 1));
  return prev;
}