Skip to content

A package for working with repeating DateTime patterns like the same day every month clamped to the month length.

License

Notifications You must be signed in to change notification settings

FMorschel/due_date

Repository files navigation

DueDate

pub package GitHub License

GitHub Sponsors Buy Me A Coffee donate button

A package for working with repeating DateTime patterns.

Ever wanted to create a new DateTime with, let's say, the same day in month? But the day is 31 and next month only has 30, so you go to 30 and the next day is lost because then you have no variable to save the original's month day? With DueDateTime this managing is done for you without any headaches.

Or maybe you have to check if some DateTime is inside a certain period of time and always have to manually get, let's say, the start and end of the week and process it yourself. Now you can use PeriodGenerators and Period methods to work it out for you.

Features

Examples of what this package can do:

DueDateTime

final date = DateTime(2022, DateTime.january, 31);
DueDateTime dueDate = DueDateTime.fromDate(date);
dueDate = dueDate.addMonths(1); // February 28th, 2022
dueDate = dueDate.addMonths(1); // March 31th, 2022

Period

final period1 = Period(
  start: DateTime(2022, DateTime.january, 1),
  end: DateTime(2022, DateTime.january, 5),
);
final period2 = Period(
  start: DateTime(2022, DateTime.january, 3),
  end: DateTime(2022, DateTime.january, 7),
);
if (period1.overlapsWith(period2)) {
  print('The two periods overlap.');
} else {
  print('The two periods do not overlap.');
}

PeriodGenerator

final hourGenerator = HourGenerator();
final secondGenerator = SecondGenerator();
final now = DateTime.now();
final currentHour = hourGenerator.of(now);
final currentSecond = secondGenerator.of(now);
print('The current hour is $currentHour.');
print('The current second is $currentSecond.');
final nextHour = currentHour.getNext(hourGenerator);
final nextSecond = currentSecond.getNext(secondGenerator);
print('The next hour is $nextHour.');
print('The next second is $nextSecond.');
final previousHour = currentHour.getPrevious(hourGenerator);
final previousSecond = currentSecond.getPrevious(secondGenerator);
print('The previous hour is $previousHour.');
print('The previous second is $previousSecond.');

Getting started

On your pubspec.yaml file, add this package to your dependencies:

  dependencies:
    due_date: ^2.0.0

Import one of the, or both, package libraries on your code:

import 'package:due_date/due_date.dart';
import 'package:due_date/period.dart';

Usage

Longer examples at /example folder.

final date = DateTime(2022, DateTime.january, 31);
DueDateTime dueDate = date.dueDateTime; // 2022-01-31
dueDate = dueDate.next; // 2022-02-28
dueDate = dueDate.next; // 2022-03-31

Additional information

See the API docs here.

Find more information at https://github.com/FMorschel/due_date.

Contribute to the package by creating a PR at https://github.com/FMorschel/due_date/pulls.

File issues at https://github.com/FMorschel/due_date/issues.

Discuss related topics at https://github.com/FMorschel/due_date/discussions.

Alternatives/Inspiration

  • pub.dev -> time

    • Made me start thinking about this package and how I could improve it with issues and discussions on the GitHub repo.
  • DateUtils -> Flutter

    • Inspired me to create the Period class and its methods. As well as some Every classes.

What makes this different

This package is focused on working with DateTime objects and their patterns. It is not a calendar package, but it can be used to create one. It is also not a package to work with timezones. It is focused on the DateTime object itself. It does not have any timezone related methods. For that, you may want to check out the timezone package.

This is not a package to work with Duration objects. It is focused on DateTime objects and their patterns. For that, you may want to check out the time package.

This package is not a package to work with DateTime objects and their formatting. It is focused on DateTime objects and their patterns. For that, you may want to check out the intl package.

This package is not intended to be a replacement for the DateTime class. It is intended to be a complement to it.

The time package is a great package to work with Duration and DateTime objects. It uses extension methods to add functionality to the DateTime class. This package is not intended to be a replacement for the time package. It is intended to be a complement to it.

License

This package is licensed under the MIT license. See the LICENSE file for details.

About

A package for working with repeating DateTime patterns like the same day every month clamped to the month length.

Topics

Resources

License

Stars

Watchers

Forks