format method
格式化
formatStr
格式化字符串
locale
语言包
dayFormat
文字格式化 小于10s= 刚刚 今年
当月几天前 往月几月前 去年
去年 'MM-DD HH:mm:ss 默认字符串
dayFormat 可以传bool 类型 也可以传一个带参的Function 自定义实现你的功能 默认false
Implementation
String format([
String formatStr = Location.defaultFormatStr,
String locale = '',
var dayFormat = false,
]) {
if (formatStr == '') formatStr = Location.defaultFormatStr;
if (_datetime == null) return '';
if (locale == '') {
locale = _localeName;
}
Locale _locale = getLocale(locale);
if (!_matchers.containsKey("W")) {
_matchers.addAll({
"W": getWeek(locale)['num'].toString(),
"WW": getWeek(locale)['text'],
});
}
if (!_matchers.containsKey("MMM")) {
if (_locale.monthStart == 0) {
_matchers
.addAll({"MMM": _locale.monthAbbreviations[int.parse(_month) - 1]});
} else if (_locale.monthStart == 1) {
_matchers
.addAll({"MMM": _locale.monthAbbreviations[int.parse(_month)]});
}
}
String s = formatStr.replaceAllMapped(Location.regexFormat, (match) {
if (match.groupCount > -1) {
String v = match.group(0).toString();
if (_matchers[v] != null) {
return _matchers[v].toString();
}
}
return '';
});
if (_matcherstatic.isNotEmpty) {
_matcherstatic.forEach((key, value) {
s = s.replaceAll(key, _matchers[key].toString());
});
}
if (dayFormat is bool && dayFormat) {
Dayfl newDate = Dayfl();
if (newDate.isSame(this, DateLocationEnum.year) &&
newDate.isAfter(this)) {
int timeX = newDate.valueOf! - valueOf!;
if (timeX < 10000) {
s = _locale.formatText['ltTenSec'] ?? '';
}
if (newDate.isSame(this, DateLocationEnum.month)) {
timeX = newDate.difference(this).inHours;
timeX = (timeX / 24).truncate();
s = '$timeX' + (_locale.formatText['ltDay'] ?? '');
if (newDate.difference(this).inHours - int.parse(newDate.hour) > 0) {
s = _locale.formatText['yesterday'] ?? '';
}
} else {
timeX = int.parse(newDate.month) - int.parse(month);
s = '$timeX' + (_locale.formatText['ltMonth'] ?? '');
}
} else if (newDate.isAfter(this)) {
int timeX = int.parse(newDate.year) - int.parse(year);
if (timeX > 0 && timeX < 2) {
s = (_locale.formatText['yesteryear'] ?? '') +
' ${format('MM-DD HH:mm:ss')}';
}
}
} else if (dayFormat is DayFormatFunc) {
s = dayFormat(this);
}
return s;
}