dart_date 1.0.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 79

dart_date #

Dart Extensions for DartTime

dart_date provides the most comprehensive, yet simple and consistent toolset for manipulating Dart dates.

Inspired by date-fns

Usage #

  • Use instance.method() for added methods.
  • Use instance.property for added getter properties.
  • Use Date.method() for added static methods.
  • Use Date.property for added static getter properties.
DE (String): Heute ist 18-Juni-2020
DE (Date): 2020-06-18 00:00:00.000
ES (Timeago): hace 3 meses
EN (Format): June 18 2020, 8:33:52 AM
.UTC: 2006-06-06 11:06:06.006006Z
Closest to now (2020-06-18 08:33:52.683698): 2020-06-16 08:33:52.610450 (2 days ago)
true
Human String: Thu Nov 20 2014 16:51:30
Yesterday: 2020-06-17 08:33:52.700225
Tomorrow: 2020-06-19 08:33:52.700579
  const pattern = '\'Heute ist\' dd-MMMM-yyyy';
  final n = DateTime.now();
  final de_String = DateTime.now().format(pattern, 'de_DE');
  final de_Date = Date.parse(de_String, pattern: pattern, locale: 'de_DE');
  print(
    'DE (String): $de_String',
  );
  print(
    'DE (Date): $de_Date',
  );

  final es_Timeago = DateTime.now().subDays(100).timeago(locale: 'es');
  print("ES (Timeago): $es_Timeago");

  final en_Format = DateTime.now().format('MMMM dd y, h:mm:ss a');
  print("EN (Format): $en_Format");

  final utc = DateTime(2006, 6, 6, 6, 6, 6, 6, 6).UTC;
  print(".UTC: $utc");

  var now = DateTime.now();
  var closest = now.closestTo(
    [
      n.nextWeek,
      n.previousDay.previousDay,
      n.previousWeek.nextDay,
      n.nextMonth,
      n.nextYear,
    ],
  );
  print('Closest to now ($now): $closest (${closest.timeago()})');

  print(Date.today is DateTime);

  print("Human String: " +
      DateTime.parse('2014-11-20T16:51:30.000Z').toHumanString());

  print("Yesterday: " + (Date.today - Duration(days: 1)).toString());
  print("Tomorrow: " + (Date.today + Duration(days: 1)).toString());

LICENSE #

MIT

API #

Check full docs

Date extension on DateTime

Properties #

clone → DateTime
endOfDay → DateTime
endOfHour → DateTime
endOfISOWeek → DateTime
endOfMinute → DateTime
endOfMonth → DateTime
endOfSecond → DateTime
endOfWeek → DateTime
endOfYear → DateTime
getDate → int
getDay → int
getDayOfYear → int
getDaysInMonth → int
getDaysInYear → int
getHours → int
getMicroseconds → int
getMicrosecondsSinceEpoch → int
getMilliseconds → int
getMillisecondsSinceEpoch → int
getMinutes → int
getMonth → int
getSeconds → int
getTime → int
getTimeZoneName → String
getTimeZoneOffset → Duration
getWeekday → int
getYear → int
isFirstDayOfMonth → bool
isFriday → bool
isFuture → bool
isLastDayOfMonth → bool
isLeapYear → bool
isMonday → bool
isPast → bool
isSaturday → bool
isSunday → bool
isThisHour → bool
isThisMinute → bool
isThisMonth → bool
isThisSecond → bool
isThisYear → bool
isThursday → bool
isToday → bool
isTomorrow → bool
isTuesday → bool
isUTC → bool
isWednesday → bool
isWeekend → bool
isYesterday → bool
Local → DateTime
nextDay → DateTime
nextMonth → DateTime
nextWeek → DateTime
nextYear → DateTime
previousDay → DateTime
previousMonth → DateTime
previousWeek → DateTime
previousYear → DateTime
secondsSinceEpoch → int
startOfDay → DateTime
startOfHour → DateTime
startOfISOWeek → DateTime
startOfMinute → DateTime
startOfMonth → DateTime
startOfSecond → DateTime
startOfWeek → DateTime
startOfYear → DateTime
timestamp → int
toLocalTime → DateTime
toUTC → DateTime
UTC → DateTime

Methods #

addDays(int amount) → DateTime
addHours(int amount) → DateTime
addMicroseconds(int amount) → DateTime
addMilliseconds(int amount) → DateTime
addMinutes(int amount) → DateTime
addMonths(int amount) → DateTime
addQuarters(int amount) → DateTime
addSeconds(int amount) → DateTime
addWeeks(int amount) → DateTime
addYears(int amount) → DateTime
closestIndexTo(Iterable<DateTime> datesArray) → int
closestTo(Iterable<DateTime> datesArray) → DateTime
compare(DateTime other) → int
diff(DateTime other) → Duration
differenceInDays(DateTime other) → int
differenceInHours(DateTime other) → int
differenceInMicroseconds(DateTime other) → int
differenceInMilliseconds(DateTime other) → int
differenceInMinutes(DateTime other) → int
differenceInSeconds(DateTime other) → int
eachDay(DateTime date) → Iterable<DateTime>
equals(DateTime other) → bool
format(String pattern, [String locale = 'en_US']) → String
isEqual(dynamic other) → bool
isSameDay(DateTime other) → bool
isSameHour(DateTime other) → bool
isSameMinute(DateTime other) → bool
isSameMonth(DateTime other) → bool
isSameOrAfter(DateTime other) → bool
isSameOrBefore(DateTime other) → bool
isSameSecond(DateTime other) → bool
isSameYear(DateTime other) → bool
isWithinInterval(Interval interval) → bool
isWithinRange(DateTime startDate, DateTime endDate) → bool
setDay(int day, [int hour, int minute, int second, int millisecond, int microsecond]) → DateTime
setHour(int hour, [int minute, int second, int millisecond, int microsecond]) → DateTime
setMicrosecond(int microsecond) → DateTime
setMillisecond(int millisecond, [int microsecond]) → DateTime
setMinute(int minute, [int second, int millisecond, int microsecond]) → DateTime
setMonth(int month, [int day, int hour, int minute, int second, int millisecond, int microsecond]) → DateTime
setSecond(int second, [int millisecond, int microsecond]) → DateTime
setYear(int year, [int month, int day, int hour, int minute, int second, int millisecond, int microsecond]) → DateTime
sub(Duration duration) → DateTime
subDays(int amount) → DateTime
subHours(int amount) → DateTime
subMicroseconds(dynamic amount) → DateTime
subMilliseconds(dynamic amount) → DateTime
subMinutes(dynamic amount) → DateTime
subMonths(dynamic amount) → DateTime
subSeconds(dynamic amount) → DateTime
subtract(Duration duration) → DateTime
subYears(dynamic amount) → DateTime
timeago({String locale, DateTime clock, bool allowFromNow}) → String
toHumanString() → String

Operators #

operator <(DateTime other) → bool
operator <=(DateTime other) → bool
operator >(DateTime other) → bool
operator >=(DateTime other) → bool
operator -(Duration other) → DateTime
operator +(Duration other) → DateTime

Static Properties #

endOfToday → DateTime
endOfTomorrow → DateTime
endOfYesterday → DateTime
startOfToday → DateTime
today → DateTime
tomorrow → DateTime
yesterday → DateTime

Static Methods #

areRangesOverlapping(DateTime initialRangeStartDate, DateTime initialRangeEndDate, DateTime comparedRangeStartDate, DateTime comparedRangeEndDate) → bool
compareAsc(DateTime dateLeft, DateTime dateRight) → int
compareDesc(DateTime dateLeft, DateTime dateRight) → int
fromSecondsSinceEpoch(int secondsSinceEpoch, {bool isUtc: false}) → DateTime
isDate(dynamic argument) → bool
max(DateTime left, DateTime right) → DateTime
min(DateTime left, DateTime right) → DateTime
parse(String dateString, {String pattern, String locale: 'en_US', bool isUTC: false}) → DateTime
unix(int seconds) → DateTime

Interval class #

// Constructors
Interval(DateTime start, DateTime end)

// Properties
duration → Duration
end → DateTime
hashCode → int
runtimeType → Type
start → DateTime

// Methods
contains(Interval interval) → bool
cross(Interval other) → bool
difference(Interval other) → Interval
equals(Interval other) → bool
includes(DateTime date) → bool
intersection(Interval other) → Interval
symetricDiffetence(Interval other) → List<Interval>
toString() → String
union(Interval other) → Interval

// Operators
operator <(Interval other) → bool
operator <=(Interval other) → bool
operator ==(dynamic other) → bool
operator >(Interval other) → bool
operator >=(Interval other) → bool

Duration Class #

// Operators
operator -(Duration other) → Duration
operator +(Duration other) → Duration
operator *(num other) → Duration
operator /(num other) → Duration

Changelog #

1.0.6 #

  • Improve documentation
  • Add extension operators on DateTime and Duration

1.0.5 #

  • same 1.0.4

1.0.4 #

  • pub.dev recommendations

1.0.3 #

  • Delete this if not needed and convert functions to => if possible

1.0.2 #

  • dartfmt overwriting

1.0.1 #

  • Include recommendations from pub.dev

1.0.0 #

  • !!! Broken API related to 0.x versions
  • Using new fancy dart extensions feature
  • Use .method() or .property to access extension methods / getters and Date.method() / Date.property for new static properties.

0.0.9 #

  • Changes Date.parse to accept or not a format
  • Added date.isFirstDayOfMonth getter
  • Added date.isLastDayOfMonth getter
  • Added date.isLeapYear getter

0.0.8 #

  • Adding documentation and timeago date.timeago()

example/dart_date_example.dart

import 'package:dart_date/dart_date.dart';

main(List<String> args) {
  const pattern = '\'Heute ist\' dd-MMMM-yyyy';
  final n = DateTime.now();
  final de_String = DateTime.now().format(pattern, 'de_DE');
  final de_Date = Date.parse(de_String, pattern: pattern, locale: 'de_DE');
  print(
    'DE (String): $de_String',
  );
  print(
    'DE (Date): $de_Date',
  );

  final es_Timeago = DateTime.now().subDays(100).timeago(locale: 'es');
  print("ES (Timeago): $es_Timeago");

  final en_Format = DateTime.now().format('MMMM dd y, h:mm:ss a');
  print("EN (Format): $en_Format");

  final utc = DateTime(2006, 6, 6, 6, 6, 6, 6, 6).UTC;
  print(".UTC: $utc");

  var now = DateTime.now();
  var closest = now.closestTo(
    [
      n.nextWeek,
      n.previousDay.previousDay,
      n.previousWeek.nextDay,
      n.nextMonth,
      n.nextYear,
    ],
  );
  print('Closest to now ($now): $closest (${closest.timeago()})');

  print(Date.today is DateTime);

  print("Human String: " +
      DateTime.parse('2014-11-20T16:51:30.000Z').toHumanString());

  print("Yesterday: " + (Date.today - Duration(days: 1)).toString());
  print("Tomorrow: " + (Date.today + Duration(days: 1)).toString());

  print(Duration(days: 1) + Duration(hours: 12, minutes: 5));
  print(Duration(days: 1) - Duration(hours: 12, minutes: 5));
  print(Duration(hours: 1) * 2.5);
  print(Duration(hours: 1) / 70);
}

Use this package as a library

1. Depend on it

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


dependencies:
  dart_date: ^1.0.6

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:dart_date/dart_date.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
58
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]
79
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
intl ^0.16.1 0.16.1
timeago ^2.0.9 2.0.26
Transitive dependencies
path 1.7.0
Dev dependencies
test ^1.3.0