nextMonth method

DateTime nextMonth()

Returns a DateTime instance representing the exact date of the next coming month.

Example:

DateTime currentDate = DateTime(2024, 1, 8);
DateTime nextMonthDate = currentDate.nextMonth;
print(nextMonthDate);  // Output: 2023-2-08 00:00:00

The nextMonth extension calculates and returns a new DateTime instance with the same day and time as the original date but adjusted to the exact date of the next month.

Implementation

DateTime nextMonth() {
  final nextYear = month == 12 ? year + 1 : year;
  final nextMonthValue = month == 12 ? 1 : month + 1;
  return DateTime(
    nextYear,
    nextMonthValue,
    day,
    hour,
    minute,
    second,
    millisecond,
    microsecond,
  );
}