时间戳转成中文格式的时间

  1. String formatCNDate(int dateValue, {bool withDetail:true}) {
  2. /// dateValue: 需要格式的时间戳, withDetail: 是否返回时分秒
  3. if(dateValue != null) {
  4. var date = new DateTime.fromMillisecondsSinceEpoch(dateValue);
  5. if(withDetail) {
  6. return '${date.year}年${date.month}月${date.day}日 ${date.hour.toString().padLeft(2, "0")}:${date.minute.toString().padLeft(2, "0")}:${date.second.toString().padLeft(2, "0")}';
  7. }else {
  8. return '${date.year}年${date.month}月${date.day}日';
  9. }
  10. }else {
  11. return '';
  12. }
  13. }

音视频播放时间格式化

  1. String formatSeconds(int seconds) {
  2. String toDigits(int n) => n.toString().padLeft(2, "0");
  3. Duration duration = Duration(seconds: seconds)
  4. String toDigitMinutes = twDigits(duration.inMinutes);
  5. String toDigitSeconds = toDigits(duration.inSeconds.remainder(60));
  6. return "$toDigitMinutes:$toDigitSeconds";
  7. }

计算时间差

  1. String workDuration (int endValue) {
  2. /// endValue: 结束时间戳
  3. var endTime = new DateTime.fromMillisecondsSinceEpoch(endValue);
  4. var nowTime = new DateTime.now();
  5. if(nowTime.isBefore(endTime)) {
  6. var diff = nowTime.difference(endTime);
  7. var diffTime = diff.toString().split('.')[0];
  8. return diffTime.replaceAll('-', '');
  9. }else {
  10. return '00:00:00';
  11. }
  12. }