dateable 2.1.1+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 73

📆 Dateable #

Pub Tests Coverage Status

A Dart package to help you with managing dates easily. Can be used to store, format, convert, construct, parse and serialise dates. Calendar correctness is guaranteed by the usage of DateTime 's system under the hood.

⚙️ Import #

In your .dart files:

import 'package:dateable/dateable.dart';

⚗️ Usage #

👷 Constructors: #

Variety of different constructors allows for great flexibility and interoperability with other types.

final date = Date(31, 12, 2019);
final date = Date.fromDateTime(DateTime(2019, 12, 31, 19, 1)); // Time of day is truncated
final date = Date.parseIso8601('2019-12-31T18:23:48.956871'); // Time of day is truncated
final date = Date.parse('31122019');
final date = Date.today();
final date = Date.yesterday();
final date = Date.tomorrow();

And a handy DateTime extension:

final date = DateTime(2019, 12, 31, 13, 26).toDate(); // Time of day is truncated

All of the above result in the same date object!

📅 Getters: #

There are three getters. Simple and easy.

final date = Date(11, 3, 2002);
print(date.day); // Prints 11
print(date.month); // Prints 3
print(date.year); // Prints 2002

↔️ Conversion methods: #

Date allows for seamless and easy conversions to most commonly used representations!

final date = Date(11, 3, 2002);
final dateTime = date.toDateTime(); // Time of day is set to zeros
print(date.toIso8601()); // Prints 2002-03-11T00:00:00.000
print(date.toString()); // Prints 11032002

📊 Comparisions: #

Comparisions work just like in your well-known DateTime objects!

final earlier = Date(11, 3, 2002);
final later = Date(21, 9, 2004);
print(earlier.isBefore(later)); // True
print(later.isAfter(earlier)); // Also true

On top of this, there are also operators > (is after) , < (is before), <=, >= and ==.

Here comes another handy DateTime extension:

DateTime(2002, 3, 11, 14, 56, 28).isTheSameDate(Date(11, 3, 2002));

But if all you want is to check if your Date is nearby, here you are.

final date = Date(11, 3, 2002);
date.isToday();
date.isYesterday();
date.isTomorrow();

📰 Formatting: #

You can format your Dates to Strings both with top-level constants and with String literals:

  • yyyy - 4 digit year, i.e. 1997
  • yy - 2 digit year, i.e. 97
  • mm - 2 digit month, i.e. 03
  • dd - 2 digit day, i.e. 11

Both of the below options are correct:

Date(11, 3, 2002).format([dd, '-', mm, '-', yyyy])
Date(11, 3, 2002).format(['dd', '-', 'mm', 'yyyy'])

🔨 Modifiers: #

Last but not least, there is a set of useful modifiers. Every Date object is immutable by default, so each of them creates a new Date object.

date.addDays(2) == date + 2 // Always true
date.subtractDays(7) == date - 7 // Also always true

You can also use the idiomatic copyWith function.

date.copyWith(day: 21, month: 9);

Sorting an Iterable of Dates chronologically is even easier:

[Date(21, 9, 2004), Date(24, 12, 2006), Date(11, 3, 2002)].sort((a, b) => a.compareTo(b));

Now the list is [Date(11, 3, 2002), Date(21, 9, 2004), Date(24, 12, 2006)].

🐛 Contributing / bug reporting #

Contributions and bug reports are welcome! Feel free to open an issue or create a pull request.

📖 License #

This package is distributed under MIT license.

2.1.1+1 #

  • Updated meta dependency version.

2.1.1 #

  • Fixed a typo in README.

2.1.0 #

  • Added assertion error messages.
  • Fixed typos in documentation.

2.0.0 #

  • BREAKING: Added null check asserts in constructors and methods.
  • Added format method and top-level constants dd, mm, yyyy and yy as its helpers.

1.3.0 #

  • Added today, tomorrow and yesterday constructors.

1.2.0+1 #

  • Added test coverage badge to README.

1.2.0 #

  • Added isToday member function.
  • Added isTomorrow member function.
  • Added isYesterday member function.

1.1.0 #

  • Added copyWith member function.
  • Added isTheSameDate DateTime extension.
  • Added isTheSameDate member function.

1.0.0+4 #

  • Minor correction in CHANGELOG.

1.0.0+3 #

  • Minor corrections in README file.

1.0.0+2 #

  • Minor corrections in README file.

1.0.0+1 #

  • Minot changes to better comply with pana requirements.

1.0.0 #

  • Initial version.

example/dateable_example.dart

import 'package:dateable/dateable.dart';

void main() {
  // The most basic constructor.
  final date = Date(21, 3, 2002);

  // You can also use [fromDateTime] constructor...
  if (date == Date.fromDateTime(DateTime(2002, 3, 21))) {
    print('True!');
  }

  // ...or use an extension...
  if (date == DateTime(2002, 3, 21).toDate()) {
    print('Also true!');
  }

  // ...or parse from [String] ddmmyyyy...
  if (date == Date.parse('21032002')) {
    print('This is awesome.');
  }

  // ...or even parse from an ISO8601 [String]!
  if (date == Date.parseIso8601('2002-03-21T14:35:26.896')) {
    print('Very useful.');
  }

  // Do subtractDays and operator- have the same meaning?
  if (Date(1, 1, 2020) - 2 == Date(1, 1, 2020).subtractDays(2)) {
    print('Yes, they do!');
  }

  // The same with addDays and operator+.
  if (Date(31, 12, 2021) + 18 == Date(1, 1, 2022).addDays(17)) {
    print(':)');
  }

  // You can also use operators >, <, >=, <= and ==...
  if (Date(21, 3, 2002) < Date(21, 9, 2004)) {
    print('Spoiler: it is true.');
  }

  // ...as well as isAfter() and isBefore()!
  if (Date(21, 3, 2002).isBefore(Date(21, 9, 2004))) {
    print('This is also true.');
  }

  // Checking if [DateTime] is on the same day as your [Date]
  // is incredibly easy.
  if (DateTime(2002, 3, 11, 14, 6).isTheSameDate(Date(11, 3, 2002))) {
    print('A really nice extension.');
  }

  // Checking if the [Date] is today, tomorrow or yesterday is even easier.
  if (date.isToday() || date.isTomorrow() || date.isYesterday()) {
    print('The star is born.');
  }

  // You also gain access to [today], [tomorrow] and [yesterday] constructors.
  print(Date.yesterday().toString() +
      Date.today().toString() +
      Date.tomorrow().toString());

  // There are also some nice conversions:
  if (Date(21, 3, 2002).toDateTime() == DateTime(2002, 3, 21)) {
    print('Nice.');
  }
  if (Date(21, 3, 2002).toIso8601() == '2002-03-21T00:00:00.000') {
    print('Nice!');
  }
  if (Date(21, 3, 2002).toString() == '21032002') {
    print('<3');
  }

  // You can enjoy the copyWith function!
  if (Date(11, 3, 2002).copyWith(day: 21, month: 9) == Date(21, 9, 2002)) {
    print('Useful for short, idiomatic mutations.');
  }

  // Formatting is as easy as it gets!
  print(date.format([dd, '-', mm, '-', yyyy]));

  // Finally, due to the implementation of [Comparable] interface,
  // you can sort easily:
  final result = [Date(21, 9, 2004), Date(24, 12, 2006), Date(11, 3, 2002)];
  result.sort((a, b) => a.compareTo(b));
  print(result); // Prints [11032002, 21092004, 24122006]
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dateable: ^2.1.1+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dateable/dateable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
45
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
meta ^1.2.2 1.2.2 1.3.0-nullsafety
Dev dependencies
pedantic ^1.9.2
test ^1.15.2
test_coverage ^0.4.3