Skip to content

Commit

Permalink
Adjust months and years based on the days overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
chayanforyou committed Sep 5, 2023
1 parent 29800d5 commit fee1271
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
25 changes: 24 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,32 @@ void main() {
// You next birthday will be in Years: 4, Months: 4, Days: 0

// Add time to any date
DateTime date = AgeCalculator.add(
DateTime date;

print('\nAdd Example:');
date = AgeCalculator.add(
date: DateTime(2021, 1, 2),
duration: DateDuration(years: 5, months: 2, days: 1));
print(date);
// 2026-03-03 00:00:00.000

date = AgeCalculator.add(
date: DateTime(2023, 2, 28),
duration: DateDuration(months: 6));
print(date);
// 2024-02-29 00:00:00.000

// Subtract time to any date
print('\nSubtract Example:');
date = AgeCalculator.subtract(
date: DateTime(2021, 1, 2),
duration: DateDuration(years: 5, months: 2, days: 1));
print(date);
// 2015-11-01 00:00:00.000

date = AgeCalculator.subtract(
date: DateTime(2023, 8, 31),
duration: DateDuration(months: 6));
print(date);
// 2023-02-28 00:00:00.000
}
32 changes: 26 additions & 6 deletions lib/age_calculator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library age_calculator;

import 'dart:math';

class DateDuration {
int days;
int months;
Expand Down Expand Up @@ -95,13 +97,19 @@ class AgeCalculator {
/// add method
static DateTime add(
{required DateTime date, required DateDuration duration}) {
int years = date.year + duration.years;
years += ((date.month + duration.months) ~/ DateTime.monthsPerYear);
int months = ((date.month + duration.months) % DateTime.monthsPerYear);

int days = date.day + duration.days - 1;
var newDateTime = date.add(Duration(days: duration.days));
newDateTime = _addMonths(newDateTime, duration.months);
newDateTime = _addMonths(newDateTime, duration.years * 12);
return newDateTime;
}

return DateTime(years, months, 1).add(Duration(days: days));
/// subtract methods
static DateTime subtract(
{required DateTime date, required DateDuration duration}) {
var newDateTime = date.subtract(Duration(days: duration.days));
newDateTime = _addMonths(newDateTime, -duration.months);
newDateTime = _addMonths(newDateTime, -duration.years * 12);
return newDateTime;
}

static DateDuration age(DateTime birthdate, {DateTime? today}) {
Expand All @@ -117,4 +125,16 @@ class AgeCalculator {
: tempDate;
return dateDifference(fromDate: endDate, toDate: nextBirthdayDate);
}

static DateTime _addMonths(DateTime dateTime, int months) {
final modMonths = months % 12;
var newYear = dateTime.year + ((months - modMonths) ~/ 12);
var newMonth = dateTime.month + modMonths;
if (newMonth > 12) {
newYear++;
newMonth -= 12;
}
final newDay = min(dateTime.day, daysInMonth(newYear, newMonth));
return DateTime(newYear, newMonth, newDay);
}
}

0 comments on commit fee1271

Please sign in to comment.