flutter 几秒前, 几分钟前, 几小时前, 几天前...
生活随笔
收集整理的這篇文章主要介紹了
flutter 几秒前, 几分钟前, 几小时前, 几天前...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Show me the code!!!
class RelativeDateFormat {static final num ONE_MINUTE = 60000;static final num ONE_HOUR = 3600000;static final num ONE_DAY = 86400000;static final num ONE_WEEK = 604800000;static final String ONE_SECOND_AGO = "秒前";static final String ONE_MINUTE_AGO = "分鐘前";static final String ONE_HOUR_AGO = "小時前";static final String ONE_DAY_AGO = "天前";static final String ONE_MONTH_AGO = "月前";static final String ONE_YEAR_AGO = "年前";//時間轉(zhuǎn)換static String format(DateTime date) {num delta = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;if (delta < 1 * ONE_MINUTE) {num seconds = toSeconds(delta);return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;}if (delta < 45 * ONE_MINUTE) {num minutes = toMinutes(delta);return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;}if (delta < 24 * ONE_HOUR) {num hours = toHours(delta);return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;}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;} }復(fù)制代碼總結(jié)
以上是生活随笔為你收集整理的flutter 几秒前, 几分钟前, 几小时前, 几天前...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDBC——Java连接关系型数据库
- 下一篇: VS 添加文件添加文件成链接