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

插件详情v1.2.2

any_date

一个用于将字符串解析为 DateTime 的包,支持任何格式。轻松解析任意格式和任意语言的日期,同时始终尊重 ISO 和 RFC 格式。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
6770
喜欢数
17
Pub 分数
160

安装插件

flutter pub add any_date

插件介绍

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

AnyDate 是一个 Flutter/Dart 包,旨在简化将字符串解析为 DateTime 对象的过程,支持任何日期格式和语言。它能自动检测并处理各种输入格式,同时优先采用 ISO 和 RFC 标准。适用于接收格式不一致或未知日期的应用程序,可减少样板代码并提高可靠性。该包可在所有主要平台上运行,使其在跨平台开发中具有高度灵活性。

适合场景

  • 从表单中解析用户输入的日期
  • 处理国际日期格式
  • 将旧版日期字符串转换为 DateTime
  • 自动检测日志中的日期格式
  • 支持多语言日期输入

主要特点

  • 自动检测日期格式
  • 支持多种语言
  • 遵守 ISO 和 RFC 标准
  • 跨平台兼容性
  • 所需配置最少
  • 强大的错误处理

适合对象

  • 具有动态日期输入的应用程序
  • 国际化应用
  • 数据迁移工具
  • 日志解析工具
  • 需要灵活日期解析的开发工具

使用建议

  • 大规模解析时可能有性能开销
  • 不适用于严格的格式验证
  • 依赖启发式方法,可能会误解模糊的日期
  • 对边缘情况格式的自定义有限
  • 在格式不确定的情况下最佳使用

原始 README

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

PubStats Popularity

Package to improve DateTime manipulation, especially by allowing parsing any format. Heavily inspired by python's dateutil package.

Summary

In a glance, these are the features:

  1. Easy parsing of a String with many date formats into a DateTime object
  2. Same flexibility supported in almost any Locale. Not just English, but any language and culture
  3. Always compliant with ISO 8601 and major RFCs (822, 2822, 1123, 1036 and 3339), regardless of Locale
  4. Supports UNIX time in either seconds, milliseconds, microseconds, or nanoseconds since epoch

Basic usage

Usage is simple, use the AnyDate() constructor to create a parser with the desired settings, and use it to parse any String into DateTime, regardless of the format.

Note that, in order to resolve ambiguity, some settings are required either on the AnyDate() constructor or on the AnyDate.defaultSettings static attribute.

const parser = AnyDate();
final date = parser.parse('13 Aug 2023');
final sameDate = parser.parse('Aug 13 2023');
final stillTheSame = parser.parser('2023, August 13');
// in all cases date is parsed as DateTime(2023, 08, 13)

However, you may notice that the example above is in English. What if you want a different Locale? You can use the AnyDate.fromLocale() factory method to get the desired parser:

// American English
final parser1 = AnyDate.fromLocale('en-US');
final date1 = parser.parse('August 13, 2023');
// note that even if formatting is unusual for locale, it can still understand unambiguous dates
final sameDate = parser.parse('13 August 2023'); // this is not common for US locale, but it still parses normally


// Brazilian Portuguese
final parser2 = AnyDate.fromLocale('pt-BR');
final date2 = parser.parse('13 de Agosto de 2023');

// again, they all resolve to same DateTime value

Solving ambiguous cases

Moreover, the parser can be used to solve ambiguous cases. Look at the following example:

// what date is this? 1 Feb 2003, 2 Jan 2003, or worse 2 Mar 2001?
// this could be dd/mm/yy, mm/dd/yy or yy/mm/dd
const ambiguousDate = '01/02/03';

// the parser can be configured for all cases:
// mm/dd/yy (this is the default behaviour)
const parser1 = AnyDate();
final case1 = a.parse(ambiguousDate); // results in DateTime(2003, 1, 2);

// dd/mm/yy
const dayFirstInfo = DateParserInfo(dayFirst: true);
const parser2 = AnyDate(info: info);
final case2 = a.parse(ambiguousDate); // results in DateTime(2003, 2, 1);

// yy/mm/dd
const yearFirstInfo = DateParserInfo(yearFirst: true);
const parser3 = AnyDate(info: info);
final case3 = a.parse(ambiguousDate); // results in DateTime(2001, 2, 3);

Using a Locale based parser also allows you to solve ambiguity based on that culture:

// same example:
const ambiguousDate = '01/02/03';

// American English
final parser1 = AnyDate.fromLocale('en-US');
final date1 = parser.parse(ambiguousDate); // the ambiguous date results in Jan 2, 2003 (mm/dd/yy)


// Brazilian Portuguese
final parser2 = AnyDate.fromLocale('pt-BR');
final date2 = parser.parse(ambiguousDate); // the ambiguous date results in Feb 1, 2003 (dd/mm/yy)

Feedback appreciated 💙

Read the docs at https://el-darto.net/

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目