Calculating a date in the future or past

Dart:
Calculating a date in the future or past

How to:

Dart provides robust support for date manipulation through its DateTime class. Here’s how you can calculate future or past dates using native Dart, without needing third-party libraries.

Calculating a Future Date

To calculate a date in the future, you create a DateTime object and use the add method with the desired duration.

DateTime today = DateTime.now();
Duration tenDays = Duration(days: 10);
DateTime futureDate = today.add(tenDays);

print(futureDate); // Output: 2023-04-21 14:22:35.123456 (example output, depends on current date and time)

Calculating a Past Date

For calculating a date in the past, you use the subtract method on a DateTime object with the necessary duration.

DateTime today = DateTime.now();
Duration fifteenDaysAgo = Duration(days: 15);
DateTime pastDate = today.subtract(fifteenDaysAgo);

print(pastDate); // Output: 2023-03-27 14:22:35.123456 (example output, depends on current date and time)

Using Third-Party Libraries

Though Dart’s native capabilities for date manipulation are powerful, you might find yourself needing more specific operations, like parsing or formatting dates more easily, or performing complex calculations. In such cases, the time package can be highly useful.

First, add time to your pubspec.yaml dependencies:

dependencies:
  time: ^2.0.0

Then, you can use it to perform similar calculations with enhanced readability:

import 'package:time/time.dart';

void main() {
  DateTime today = DateTime.now();

  // Calculating a future date
  DateTime futureDate = today + 10.days;
  print(futureDate); // Output format: 2023-04-21 14:22:35.123456

  // Calculating a past date
  DateTime pastDate = today - 15.days;
  print(pastDate); // Output format: 2023-03-27 14:22:35.123456
}

These examples illustrate basic date manipulations in Dart, including adding and subtracting time to or from a current date, demonstrating how effortlessly dates can be managed in Dart applications.