format method
Formats the date range into a string using the specified format.
The fmt
parameter is a template string that can contain the placeholders {{start}}
and {{end}}
.
These placeholders will be replaced with the formatted start and end dates, respectively.
The dateFormat
parameter specifies the format for the dates using the DateFormat
class.
The locale
parameter specifies the locale for formatting the dates.
The inclusiveTag
and exclusiveTag
parameters are used to indicate whether the start and end dates are inclusive or exclusive.
If not provided, no tag is added.
The startInclusive
and endInclusive
parameters can be used to override the default inclusivity of the range.
Example:
DateRange(DateTime(2023, 1, 1), DateTime(2023, 1, 3), startInclusive: true, endInclusive: false)
.format("Range: {{start}} - {{end}}", "dd.MM.yyyy", inclusiveTag: "[", exclusiveTag: ")");
// Output: "Range: [01.01.2023 - 03.01.2023)"
Note: initializeDateFormatting() should be called before using this method.
Init Date Formatting locale in caller's call before calling DateRange.format(), e.g.
initializeDateFormatting()
.then((_) => daterange.format("{{start}} - {{end}}", "E dd.MM.", locale: "cs_CZ");
Implementation
String format(String fmt, String dateFormat, {String? locale, String? inclusiveTag, String? exclusiveTag,
bool? startInclusive, bool? endInclusive}) {
final DateFormat df = DateFormat(dateFormat, locale);
final DateTime? s = start(inclusive: startInclusive ?? _startInclusive);
final DateTime? e = end(inclusive: endInclusive ?? _endInclusive);
String buffer = fmt
.replaceAll(
'{{start}}',
s == null
? ''
: df.format(s) +
(startInclusive ??_startInclusive
? inclusiveTag ?? ''
: exclusiveTag ?? ''))
.replaceAll(
'{{end}}',
e == null
? ''
: df.format(e) +
(endInclusive ??_endInclusive
? inclusiveTag ?? ''
: exclusiveTag ?? ''));
return buffer;
}