With shiny extensions, if you have ever written something like this, then look no further:
final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4);
dependencies:
time: "^1.0.0"
import 'package:time/time.dart';
final Duration tenMinutes = 10.minutes;
final DateTime afterTenMinutes = DateTime.now() + 10.minutes;
final Duration tenMinutesAndSome = 10.minutes + 15.seconds;
final int tenMinutesInSeconds = 10.minutes.inSeconds;
final DateTime tenMinutesFromNow = 10.minutes.fromNow;
You can perform all basic arithmetic operations on Duration
as you always have been:
final Duration interval = 10.minutes + 15.seconds - 3.minutes + 2.hours;
final Duration doubled = interval * 2;
You can also use these operations on DateTime
:
final DateTime oneHourAfter = DateTime() + 1.hours;
Duration
is easily convertible as it always has been:
final int twoMinutesInSeconds = 2.minutes.inSeconds;
You can also convert Duration
to DateTime
, if needed:
final DateTime timeInFuture = 5.minutes.fromNow;
final DateTime timeInPast = 5.minutes.ago;
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.
MIT License