सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
एक फ्लटर/डार्ट पैकेज जो डार्ट के DateTime ऑब्जेक्ट्स को फॉर्मेट करने के लिए उपयोगी उपकरण प्रदान करता है। यह मानक तारीख/समय प्रणाली के साथ-साथ सापेक्ष समय विचलन (उदाहरण के लिए '2 मिनट पहले') को समर्थित करता है। विभिन्न प्लेटफ़ॉर्मों पर ऐप में तारीख प्रस्तुति को सरल बनाने के लिए डिज़ाइन किया गया है, इसमें जटिल हाथ से फॉर्मेटिंग की आवश्यकता नहीं होती है। पैकेज हल्का है और मौजूदा प्रोजेक्ट में बिना किसी दिक्कत के एकीकृत हो सकता है।
उपयोग
उपयोगकर्ता गतिविधि समयांक दिखाएं
चैट या सोशल मीडिया ऐप्स में तारीख़ को प्रारूपित करना
एक घटना के बाद से सापेक्ष समय दिखाएं
बहुभाषी एप्लिकेशन में तारीख प्रदर्शन का स्थानीयीकरण
UI संघटकों में तारीख स्ट्रिंग उत्पन्न करने को सरल बनाना
मुख्य विशेषताएँ
सापेक्ष समय प्रारूपण का समर्थन करता है
मानक तिथि/समय पैटर्न को संभालता है
हल्का और एकीकरण में आसान
क्रॉस-प्लेटफॉर्म संगतता
सामान्य प्रारूपण की आवश्यकता के लिए सरल API
इनके लिए उपयुक्त
मानव-पढ़ने योग्य समयस्टैम्प की आवश्यकता वाले ऐप्स
न्यूनतम तारीख प्रारूपण समाधान खोज रहे विकासकर्मी
कई प्लेटफॉर्म के लिए लक्षित प्रोजेक्ट
अक्सर तिथि प्रदर्शन वाले UI-भारी अनुप्रयोग
कोड सरलता और रखरखाव के प्राथमिकता देने वाली टीमें
विचार योग्य बातें
मूलभूत स्वरूपण विकल्पों तक सीमित
कोई निर्मित लोकलाइजेशन समर्थन नहीं है
डिफ़ॉल्ट सिस्टम क्षेत्रीय सेटिंग्स पर निर्भर करता है
कस्टम फॉर्मेट के लिए अतिरिक्त सेटअप की आवश्यकता हो सकती है
जटिल अंतरराष्ट्रीय तारीख प्रबंधन के लिए उपयुक्त नहीं
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
date_time_format
A utility class, and extension methods, for formatting Dart's [DateTime]
object using standard date/time notation or as a relative time offset.
date_time_format exposes 2 utility methods, [format] and [relative], that
are added to the [DateTime] class as extension methods or can be accessed
via the [DateTimeFormat] utility class.
[format] and [relative] format [DateTime] objects as defined by standard
date/time notation or as relative to the current or a provided [DateTime],
respectively.
See the Date/Time Notations section below for a complete list of supported
formatting notations.
In addition to the formatting utilities, a variety of date/time notation
formatting constants are pre-defined in the following classes: DateTimeFormats
contains standard date/time formatting notations. DateFormats
and TimeFormats contain common date-only and time-only
formatting constants, respectively. Additional American and European date/time
formatting constants can be found in the AmericanDateFormats,
AmericanDateTimeFormats, EuropeanDateFormats, and EuropeanDateTimeFormats classes.
Standard Formatting
Note: Each example below shows the extension method being utilized, and
the utility method being used on the next line. Both methods will return the
same output.
final dateTime = DateTime.now();
// 2019-10-15T19:42:05-08:00print(dateTime.format());
print(DateTimeFormat.format(dateTime));
// October 15, 2019 7:42 pmprint(dateTime.format(DateTimeFormats.american));
print(DateTimeFormat.format(dateTime, format: DateTimeFormats.american));
// 15/Oct/2019:19:42:05 -0700print(dateTime.format(DateTimeFormats.commonLogFormat));
print(DateTimeFormat.format(dateTime, format: DateTimeFormats.commonLogFormat));
// Tuesday, October 15, 2019print(dateTime.format(AmericanDateFormats.dayOfWeek));
print(DateTimeFormat.format(dateTime, format: AmericanDateFormats.dayOfWeek));
// Tue, Oct 15, 19:42print(dateTime.format('D, M j, H:i'));
print(DateTimeFormat.format(dateTime, format: 'D, M j, H:i'));
Relative Formatting
By default, [DateTimeFormat.relative] returns the time offset rounded to the nearest
whole unit of time.
To compare the provided [DateTime] to another [DateTime] object, the [relativeTo]
parameter can be set.
final timeOffset1 = dateTime.subtract(Duration(minutes: 5));
final timeOffset2 = dateTime.add(Duration(minutes: 5));
// 5 minutesprint(DateTimeFormat.relative(timeOffset1));
// 10 minutesprint(DateTimeFormat.relative(timeOffset1, relativeTo: timeOffset2));
Level of Precision:
The [levelOfPrecision] parameter sets the degrees of removal from the largest
interval counted that will be included in the count. I.e. minutes are 1
degree removed from hours, and seconds are 2 degrees removed from hours but
only 1 degree removed from minutes.
Note: Weeks can be excluded as an interval of time by setting the
[excludeWeeks] parameter to true, and will not be counted as a degree
of time in regards to [levelOfPrecision].
The minimum and maximum intervals to include in the count can be specified by
the [minUnitOfTime] and [maxUnitOfTime] respectively. The largest possible
interval of time being a year, and the smallest being a microsecond.
By default, the smallest unit of time returned is a second, to include milliseconds
or microseconds the [minUnitOfTime] parameter must be set to [UnitOfTime.milliseconds]/
[UnitOfTime.microseconds]. Note: Milliseconds and microseconds may be returned slightly
off from the expected values if the function takes longer to process than those intervals
of time.
Rounding, which is enabled by default, can be disabled by setting the [round]
parameter to false, in which case any measure of time that isn't included
in the smallest returned interval will be truncated.
[formatAfter] can be set to a [Duration] of time to return a standard formatted
date/time stamp, via the [DateTimeFormat.format] method, if the difference in
time is greater than the duration specified.
By default, date/time stamps will be formatted to the [AmericanDateTimeStamp.abbrWithComma]
constant. A different formatting notation can be provided via the [format] parameter.
final timeOffset = dateTime.subtract(Duration(days: 762, hours: 6, minutes: 30));
// 2 yearsprint(DateTimeFormat.relative(timeOffset));
// Apr 29, 2018 6:27 amprint(DateTimeFormat.relative(timeOffset, formatAfter: Duration(days: 365)));
// 6:27 am · Apr 29, 2018print(DateTimeFormat.relative(timeOffset, formatAfter: Duration(days: 365),
format: r'g:i a · M j, Y'));
Prepending & Appending Text:
[prependIfBefore] and [appendIfAfter] can be used to conveniently prepend and/or append a
[String] before or after the returned relative time offset if the formatted [DateTime] occurs
before or after [relativeTo] (which defaults to DateTime.now()), respectively.
// 5 days agoprint(DateTimeFormat.relative(dateTime.subtract(Duration(days: 5)),
prependIfBefore: 'In', appendIfAfter: 'ago'));
// In 5 daysprint(DateTimeFormat.relative(dateTime.add(Duration(days: 5)),
prependIfBefore: 'In', appendIfAfter: 'ago'));
Return X Below Minimum Unit of Time:
The [ifNow] parameter can be set to return a value if the difference in time is less
than [minUnitOfTime], otherwise, an empty string will be returned.