项目作者: SugaR256

项目描述 :
A Dart package to help you with managing dates easily.
高级语言: Dart
项目地址: git://github.com/SugaR256/dateable.git
创建时间: 2020-05-11T11:01:18Z
项目社区:https://github.com/SugaR256/dateable

开源协议:MIT License

下载


📆 Dateable

Pub
Buy Me A Coffee

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:

  1. import 'package:dateable/dateable.dart';

⚗️ Usage

👷 Constructors

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

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

And a handy DateTime extension:

  1. 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 4 getters. Simple and easy.

  1. final date = Date(11, 3, 2002);
  2. print(date.day); // Prints 11
  3. print(date.month); // Prints 3
  4. print(date.year); // Prints 2002
  5. print(date.weekday) // Prints 1, since it's a Monday

↔️ Conversion methods

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

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

✅ Validation

You can validate your dates easily with the isValid static method.

Since the default Date constructor doesn’t throw on invalid input, and always tries to silently convert to a valid date instead (just like the DateTime constructor), you may want to perform the validation explicitly.

  1. print(Date.isValid(day: 11, month: 3, year: 2002)); // Prints true
  2. print(Date.isValid(day: 29, month: 2, year: 2021)); // Prints false
  3. print(Date.isValid(day: 40, month: 50, year: -99999)); // Prints true

📊 Comparisions

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

  1. final earlier = Date(11, 3, 2002);
  2. final later = Date(21, 9, 2004);
  3. print(earlier.isBefore(later)); // True
  4. 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:

  1. 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.

  1. final date = Date(11, 3, 2002);
  2. date.isToday();
  3. date.isYesterday();
  4. 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:

  1. Date(11, 3, 2002).format([dd, '-', mm, '-', yyyy])
  1. 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.

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

You can also use the idiomatic copyWith function.

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

Sorting an Iterable of Dates chronologically is even easier:

  1. [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.