monthsAgo method

DateTime monthsAgo(
  1. int months
)

Return the point in time months ago on the same date.

If the current day of the month isn't valid in the new month, the nearest valid day in the new month will be used.

Implementation

DateTime monthsAgo(int months) {
  var time = now();
  var month = (time.month - months - 1) % 12 + 1;
  var year = time.year - (months + 12 - time.month) ~/ 12;
  var day = clampDayOfMonth(year: year, month: month, day: time.day);
  return DateTime(year, month, day, time.hour, time.minute, time.second,
      time.millisecond);
}