Home > OS >  Is it possible to have a time interval in Flutter?
Is it possible to have a time interval in Flutter?

Time:11-04

I wondering if there is any way to get a time interval for an item in Flutter?

Example:

I have an item where I set a date when this item was last calibrated, and then I want the application to remind me (with either a push message or an email) after a set period of time for the next calibration and so on.

I use Firestore for my database.

CodePudding user response:

You may want to take a look to the Duration Dart's class.

Docs: https://api.dart.dev/stable/2.18.4/dart-core/Duration-class.html

To give you an idea, fast code example to initialize a span of time:

const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2);
print(fastestMarathon.inDays); // 0
print(fastestMarathon.inHours); // 2
print(fastestMarathon.inMinutes); // 123
print(fastestMarathon.inSeconds); // 7382
print(fastestMarathon.inMilliseconds); // 7382000

CodePudding user response:

You can look at the timeago package implementation to compare times.

dependencies:
  timeago: ^3.3.0

You can use time ago for the exact purpose and it is quite beneficial. It has multiple formats and different language support as well.

import 'package:timeago/timeago.dart' as timeago;

main() {
    final fifteenAgo = DateTime.now().subtract(Duration(minutes: 15));

    print(timeago.format(fifteenAgo)); // 15 minutes ago
    print(timeago.format(fifteenAgo, locale: 'en_short')); // 15m
    print(timeago.format(fifteenAgo, locale: 'es')); // hace 15 minutos
}
  • Related