Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust months and years based on the days overflow #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,33 @@ void main() {
// You next birthday will be in Years: 4, Months: 4, Days: 0

// Add time to any date
DateTime date = AgeCalculator.add(
date: DateTime(2021, 1, 2),
duration: DateDuration(years: 5, months: 2, days: 1));
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
}
```
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);
}
}