123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- class DateFormat {
- static const num ONE_MINUTE = 60000;
- static const num ONE_HOUR = 3600000;
- static const num ONE_DAY = 86400000;
- static const num ONE_WEEK = 604800000;
- static const String ONE_SECOND_AGO = "秒前";
- static const String ONE_MINUTE_AGO = "分钟前";
- static const String ONE_HOUR_AGO = "小时前";
- static const String ONE_DAY_AGO = "天前";
- static const String ONE_MONTH_AGO = "月前";
- static const String ONE_YEAR_AGO = "年前";
- static String formatCreateAtYYYYMMDD(String? date, {String join = "-"}) {
- if (date == null) return '';
- var time = DateTime.parse(date);
- return "${time.year}$join${time.month}$join${time.day}}";
- }
- static String formatCreateAtHHmm(String? date) {
- if (date == null) return '';
- var time = DateTime.parse(date);
- return "${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
- }
- static String formatCreateAtYYYYMMDDHHmm(String? date) {
- if (date == null) return '';
- var time = DateTime.parse(date);
- return "${time.year}-${time.month}-${time.day} ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
- }
- static String formatCreateAt(String? date) {
- if (date == null) return '';
- return format(DateTime.parse(date));
- }
- static String format(DateTime? date, {int level = 99}) {
- if(date == null)
- return "";
- return formatTime(date.millisecondsSinceEpoch ~/ 1000, level: level);
- }
- static String formatTime(num? time, {int level = 99}) {
- if(time == null)
- return "";
- num delta = DateTime.now().millisecondsSinceEpoch - time * 1000;
- if (delta <= 1000) {
- return "刚刚";
- }
- if (delta < 1 * ONE_MINUTE && level > 1) {
- num seconds = toSeconds(delta);
- return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
- }
- if (delta < 60 * ONE_MINUTE && level > 2) {
- num minutes = toMinutes(delta);
- return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
- }
- if (delta < 24 * ONE_HOUR && level > 3) {
- num hours = toHours(delta);
- return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
- }
- if(level == 99) {
- var date = DateTime.fromMillisecondsSinceEpoch(time.toInt() * 1000);
- return "${date.year}-${date.month}-${date.day}";
- }else if(level == 98) {
- var date = DateTime.fromMillisecondsSinceEpoch(time.toInt() * 1000);
- return "${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}";
- }else {
- var date = DateTime.fromMillisecondsSinceEpoch(time.toInt() * 1000);
- return "${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}";
- }
- // if (delta < 48 * ONE_HOUR) {
- // return "昨天";
- // }
- // if (delta < 30 * ONE_DAY) {
- // num days = toDays(delta);
- // return (days <= 0 ? 1 : days).toInt().toString() + ONE_DAY_AGO;
- // }
- // if (delta < 12 * 4 * ONE_WEEK) {
- // num months = toMonths(delta);
- // return (months <= 0 ? 1 : months).toInt().toString() + ONE_MONTH_AGO;
- // } else {
- // num years = toYears(delta);
- // return (years <= 0 ? 1 : years).toInt().toString() + ONE_YEAR_AGO;
- // }
- }
- static num toSeconds(num date) {
- return date / 1000;
- }
- static num toMinutes(num date) {
- return toSeconds(date) / 60;
- }
- static num toHours(num date) {
- return toMinutes(date) / 60;
- }
- static num toDays(num date) {
- return toHours(date) / 24;
- }
- static num toMonths(num date) {
- return toDays(date) / 30;
- }
- static num toYears(num date) {
- return toMonths(date) / 365;
- }
- static String toVideoTime(int second) {
- int hour = 0, min = 0, sec = 0, _second = second;
- sec = _second % 60;
- _second = _second ~/ 60;
- min = _second % 60;
- hour = _second ~/ 60;
- if(hour > 0) {
- return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
- }else{
- return "${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
- }
- }
- static String toHourTime(int second) {
- int hour = 0, min = 0, sec = 0, _second = second;
- sec = _second % 60;
- _second = _second ~/ 60;
- min = _second % 60;
- hour = _second ~/ 60;
- return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
- }
- static String toTime(int second) {
- int hour = 0, min = 0, sec = 0, _second = second;
- sec = _second % 60;
- _second = _second ~/ 60;
- min = _second % 60;
- hour = _second ~/ 60;
- return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
- }
- static String toTime1(int second) {
- int hour = 0, min = 0, sec = 0, _second = second;
- sec = _second % 60;
- _second = _second ~/ 60;
- min = _second % 60;
- hour = _second ~/ 60;
- if(hour > 0){
- return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
- }else{
- return "${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
- }
- }
- static String toTimeDateFormat(String? time) {
- if(time == null)
- return "";
- var date = DateTime.parse(time);
- return "${date.hour.toString().padLeft(2, "0")}:${date.minute.toString().padLeft(2, "0")}";
- }
- }
|