年龄

一个 Flutter 包,用于计算某人的年龄,以天、月和年为单位;此外,它还可以用于查找两个日期之间的差异。

由于原始包已经超过 2 年未更新,因此此包是原始包的扩展

入门

在您的 Flutter 项目中添加依赖项

dependencies:
  ...
  age: ^1.0.0

有关 Flutter 入门帮助,请查看在线文档。

示例

import 'package:age/age.dart';

void main() {
  DateTime birthday = DateTime(1990, 1, 20);
  DateTime today = DateTime.now(); //2020/1/24

  AgeDuration age;

  // Find out your age
  age = Age.dateDifference(
      fromDate: birthday, toDate: today, includeToDate: false);

  print('Your age is $age'); // Your age is Years: 30, Months: 0, Days: 4

  // Find out when your next birthday will be.
  DateTime tempDate = DateTime(today.year, birthday.month, birthday.day);
  DateTime nextBirthdayDate = tempDate.isBefore(today)
      ? Age.add(date: tempDate, duration: AgeDuration(years: 1))
      : tempDate;
  AgeDuration nextBirthdayDuration =
      Age.dateDifference(fromDate: today, toDate: nextBirthdayDate);

  print('You next birthday will be on $nextBirthdayDate or in $nextBirthdayDuration');
  // You next birthday will be on 2021-01-20 00:00:00.000 or in Years: 0, Months: 11, Days: 27
}

GitHub

查看 Github