Search packages, repositories and app categories

Package detailsv2.1.6

time

Type-safe DateTime and Duration calculations, powered by extensions.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
1.1M
Likes
387
Pub points
160

Install package

flutter pub add time

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The time package provides type-safe DateTime and Duration calculations in Dart, enhancing reliability and readability through intuitive extensions. It simplifies common date and time operations by offering a clean, fluent API that prevents runtime errors associated with manual date manipulation. Built with safety and developer experience in mind, it supports all major platforms including mobile, web, and desktop. The package is ideal for applications requiring precise time handling, such as scheduling, timers, or event tracking.

Use cases

  • Scheduling events
  • Calculating time differences
  • Building countdown timers
  • Handling recurring tasks
  • Managing time zones
  • Tracking durations

Key features

  • Type-safe operations
  • Extensive extension methods
  • Cross-platform support
  • Compile-time safety
  • Intuitive syntax
  • Immutable duration handling

Suitable for

  • Developers needing reliable time logic
  • Apps with complex scheduling
  • Time-based analytics
  • Real-time applications
  • Educational tools for time manipulation

Considerations

  • Requires understanding of Dart extensions
  • May increase build size slightly
  • Not suitable for low-level system time access
  • Limited to high-level time calculations
  • No built-in time zone conversion

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

⏰ Time

Build codecov pub package

With shiny extensions, if you have ever written something like this, then look no further:

final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4);

🎖 Installation

dependencies:
  time: "^2.1.6"
⚡ Import
import 'package:time/time.dart';

🎮 Usage

final Duration tenMinutes = 10.minutes;
final Duration oneHourThirtyMinutes = 1.5.hours;
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;

Iterate through a DateTime range:

final DateTime start = DateTime(2019, 12, 2);
final DateTime end = start + 1.weeks;
final DateTime tuesday = start.to(end).firstWhere((date) => date.weekday == DateTime.tuesday);

Granular comparison between DateTime fields:

final DateTime specificDate = DateTime(2021, 01, 01);
final DateTime otherDate = DateTime(2021, 02, 01);

print(specificDate.isAtSameYearAs(otherDate)); // true
print(specificDate.isAtSameMonthAs(otherDate)); // false
print(specificDate.isAtSameDayAs(otherDate)); // false

You can also delay code execution:

void doSomething() async {
  await 5.seconds.delay;
  // Do the other things
}

You can also use the popular copyWith:

final initial = DateTime(2019, 2, 4, 24, 50, 45, 1, 1);
final expected = initial.copyWith(
  year: 2021,
  month: 10,
  day: 28,
  hour: 12,
  minute: 45,
  second: 10,
  millisecond: 0,
  microsecond: 12,
);

Visit the API Reference to find out all that is available.

🐛 Bugs/Requests

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.

👏 Inspiration

  • Swift library of the same name - Time.
  • Kotlin library of the same name - Time.

⭐ License

MIT License

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package

Rankings