搜索插件、代码仓与 APP 分类

插件详情v1.0.0

age_calculator

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.3万
喜欢数
66
Pub 分数
160

安装插件

flutter pub add age_calculator

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

age_calculator 包是一个专为 Flutter 设计的实用工具,用于计算一个人的年龄(以年、月、日表示)。它还允许用户精确计算两个日期之间的差异。适用于需要年龄验证或日期比较的应用程序,例如健康应用、注册表单或活动规划工具。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,确保跨平台的一致性。它为开发者提供了一个简单直观的 API,可轻松将年龄计算功能集成到其应用程序中。

适合场景

  • 用户注册中的年龄验证
  • 在医疗应用程序中计算患者年龄
  • 根据年龄确定事件资格
  • 用于调度目的的日期比较
  • 使用日期差值跟踪里程碑

主要特点

  • 计算年龄(以年、月、日为单位)
  • 准确计算日期差异
  • 跨平台兼容性
  • 简单直观的 API
  • 支持空安全的日期输入

适合对象

  • 表单验证需要年龄检查
  • 健康与保健应用
  • 生日和纪念日追踪器
  • 基于时间的学习教育应用
  • 需要精确日期计算的应用程序

使用建议

  • 需要有效的 DateTime 输入
  • 可能需要处理闰年等边缘情况
  • 未针对非常大的日期范围进行优化
  • 仅限基于日期的计算
  • 不包含时区感知功能

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

age_calculator

A Flutter package that allows you to perform operations between dates:

  • Calculate a person's age and the time remaining to their next birthday in years, months and days.
  • Calculate differences between two dates in years, months and days.
  • Add years, months and days to any date.

Getting Started

In your flutter project add the dependency:

dependencies: ... age_calculator: ^1.0.0

For help getting started with Flutter, view the online documentation.

Example
import 'package:age_calculator/age_calculator.dart';

void main() {
  DateTime birthday = DateTime(1997, 3, 5);

  DateDuration duration;

  // Find out your age as of today's date 2021-03-08
  duration = AgeCalculator.age(birthday);
  print('Your age is $duration'); // Your age is Years: 24, Months: 0, Days: 3

  //Find out your age on any given date
  duration = AgeCalculator.age(birthday, today: DateTime(2030, 5, 1));
  print('Your age is $duration'); // Your age is Years: 33, Months: 1, Days: 26

  // Find out when your next birthday will be at 2021-03-08
  duration = AgeCalculator.timeToNextBirthday(birthday);
  print('You next birthday will be in $duration');
  // You next birthday will be in Years: 0, Months: 11, Days: 25

  // Find out when your next birthday will be on any given date
  duration = AgeCalculator.timeToNextBirthday(birthday,
      fromDate: DateTime(2021, 3, 2));
  print('You next birthday will be in $duration');
  // You next birthday will be in Years: 0, Months: 0, Days: 3

  // Find out the difference between two dates
  duration = AgeCalculator.dateDifference(
    fromDate: DateTime(2021, 1, 2),
    toDate: DateTime(2025, 5, 2),
  );
  print('The difference is $duration');
  // You next birthday will be in Years: 4, Months: 4, Days: 0

  // Add time to any date
  DateTime date = AgeCalculator.add(
      date: DateTime(2021, 1, 2),
      duration: DateDuration(years: 5, months: 2, days: 1));
  print(date);
  // 2026-03-03 00:00:00.000
}

所属分类

源码仓库

使用此插件的项目