Dart如何将分钟数转换为年、月、日、时、分格式的字符串
实现方案
核心思路
由于年、月的时长不固定(受闰年、大小月影响),无法通过固定换算系数直接计算,因此最准确的实现方式是基于日历时间偏移做差值计算:先以指定基准时间(默认用当前时间)为起点,叠加对应分钟数得到目标时间,再逐单位计算两个时间点的差值即可。
完整Dart代码实现
String formatMinutesToTime(int totalMinutes, {DateTime? baseTime}) { // 可自定义基准时间,默认使用当前时间 final DateTime startTime = baseTime ?? DateTime.now(); final DateTime endTime = startTime.add(Duration(minutes: totalMinutes)); int years = endTime.year - startTime.year; int months = endTime.month - startTime.month; int days = endTime.day - startTime.day; int hours = endTime.hour - startTime.hour; int minutes = endTime.minute - startTime.minute; // 逐层处理负差值的借位逻辑 if (minutes < 0) { minutes += 60; hours -= 1; } if (hours < 0) { hours += 24; days -= 1; } if (days < 0) { // 获取上一个月的总天数,处理大小月/闰月问题 final prevMonthLastDay = DateTime(endTime.year, endTime.month, 0); days += prevMonthLastDay.day; months -= 1; } if (months < 0) { months += 12; years -= 1; } // 组装单位文本,自动处理单复数 List<String> unitParts = []; if (years > 0) unitParts.add('$years ${years == 1 ? 'year' : 'years'}'); if (months > 0) unitParts.add('$months ${months == 1 ? 'month' : 'months'}'); if (days > 0) unitParts.add('$days ${days == 1 ? 'day' : 'days'}'); if (hours > 0) unitParts.add('$hours ${hours == 1 ? 'hour' : 'hours'}'); if (minutes > 0) unitParts.add('$minutes ${minutes == 1 ? 'minute' : 'minutes'}'); // 按要求格式拼接字符串 if (unitParts.length == 1) { return unitParts.first; } else if (unitParts.length == 2) { return '${unitParts[0]} and ${unitParts[1]}'; } else { return '${unitParts.sublist(0, unitParts.length - 1).join(', ')}, and ${unitParts.last}'; } } // 使用示例 void main() { int minutes = 1000000; String formattedTime = formatMinutesToTime(minutes); print(formattedTime); // 输出示例(随基准时间不同会有小幅差异):1 year, 10 months, 29 days, 10 hours, and 40 minutes }
可选简化版本(固定换算)
如果不需要精准对齐实际日历,可以使用固定系数换算,优点是计算速度快、结果固定,缺点是存在误差:
String formatMinutesFixed(int totalMinutes) { // 固定换算系数:1年=365天,1月=30天 const int minPerYear = 365 * 24 * 60; const int minPerMonth = 30 * 24 * 60; const int minPerDay = 24 * 60; const int minPerHour = 60; int years = totalMinutes ~/ minPerYear; int remaining = totalMinutes % minPerYear; int months = remaining ~/ minPerMonth; remaining %= minPerMonth; int days = remaining ~/ minPerDay; remaining %= minPerDay; int hours = remaining ~/ minPerHour; int minutes = remaining % minPerHour; // 单位拼接逻辑和上面一致,可直接复用 List<String> unitParts = []; if (years > 0) unitParts.add('$years ${years == 1 ? 'year' : 'years'}'); if (months > 0) unitParts.add('$months ${months == 1 ? 'month' : 'months'}'); if (days > 0) unitParts.add('$days ${days == 1 ? 'day' : 'days'}'); if (hours > 0) unitParts.add('$hours ${hours == 1 ? 'hour' : 'hours'}'); if (minutes > 0) unitParts.add('$minutes ${minutes == 1 ? 'minute' : 'minutes'}'); if (unitParts.length == 1) return unitParts.first; if (unitParts.length == 2) return '${unitParts[0]} and ${unitParts[1]}'; return '${unitParts.sublist(0, unitParts.length-1).join(', ')}, and ${unitParts.last}'; }
内容的提问来源于stack exchange,提问作者Uri PD
相关产品推荐
相关产品推荐

