我想要像这样的时差-
"2小时28分钟前"、"几秒前"、"几分钟前"、"1分钟前"、"6小时前"、"一天前"、"一周前"、"几个月前"
我将事件发生时间作为DateTime对象.我可以用DateTime.now()
得到当前时间,我想计算这2次之间的差额.我目前正在使用很多行的if Else语句来完成这项工作,比如……
if (now.year > date.year) {
//months ago
if ((12 - date.month) + now.month > 11) {
return "a year ago";
} else if ((12 - date.month) + now.month > 1) {
return "${(12 - date.month) + now.month} months ago";
} else if ((12 - date.month) + now.month == 1) {
if ((getDateCountForMonth(date.month) - date.day) + now.day > 13) {
return "a few weeks ago";
}
...}}
Is there a better and simple way to do this? I want to output like this - "2小时28分钟前"、"几秒前"、"几分钟前"、"1分钟前"、"6小时前"、"一天前"、"一周前"、"几个月前"