AnyDate 是一個 Flutter/Dart 套件,旨在簡化將字串解析為 DateTime 物件的過程,支援任何日期格式和語言。它能自動偵測並處理各種輸入格式,同時優先採用 ISO 和 RFC 標準。適用於接收格式不一致或未知日期的應用程式,可減少樣板程式碼並提升可靠性。該套件可在所有主要平台上運作,使其在跨平台開發中具有高度彈性。
適用情境
從表單中解析使用者輸入的日期
處理國際日期格式
將舊版日期字串轉換為 DateTime
自動檢測日誌中的日期格式
支援多語言日期輸入
主要特色
自動檢測日期格式
支援多種語言
遵守 ISO 和 RFC 標準
跨平台相容性
所需設定最少
強大的錯誤處理
適合對象
具有動態日期輸入的應用程式
國際化應用
資料遷移工具
日誌解析工具
需要靈活日期解析的開發工具
使用建議
大規模解析時可能有性能開銷
不適用於嚴格的格式驗證
依賴啟發式方法,可能會誤解模糊的日期
對邊緣情況格式的自訂有限
在格式不確定的情況下最佳使用
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
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:
Easy parsing of a String with many date formats into a DateTime object
Same flexibility supported in almost any Locale. Not just English, but any language and culture
Always compliant with ISO 8601 and major RFCs (822, 2822, 1123, 1036 and 3339), regardless of Locale
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 Englishfinal 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 datesfinal sameDate = parser.parse('13 August 2023'); // this is not common for US locale, but it still parses normally// Brazilian Portuguesefinal 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/ddconst 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/yyconst dayFirstInfo = DateParserInfo(dayFirst: true);
const parser2 = AnyDate(info: info);
final case2 = a.parse(ambiguousDate); // results in DateTime(2003, 2, 1);// yy/mm/ddconst 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 Englishfinal parser1 = AnyDate.fromLocale('en-US');
final date1 = parser.parse(ambiguousDate); // the ambiguous date results in Jan 2, 2003 (mm/dd/yy)// Brazilian Portuguesefinal parser2 = AnyDate.fromLocale('pt-BR');
final date2 = parser.parse(ambiguousDate); // the ambiguous date results in Feb 1, 2003 (dd/mm/yy)