将毫秒转换为小时格式

时间:2017-10-05 10:52:56

标签: javascript angular typescript

我有一个开始日期,时间和结束日期,时间,我也找到了旅行的总持续时间。输出以毫秒为单位,我需要将其转换为小时格式。通过在这里搜索其他答案,我尝试了以下但没有结果。

<md-cell *mdCellDef="let row"> {{row?.duration | formatDuration}} </md-cell>

和ts文件:

  export class StoppageComponent implements OnInit {

  constructor() {
  }

  ngOnInit() {
  }


      filter('formatDuration', function () {
        return function (input) {
            var totalHours, totalMinutes, totalSeconds, hours, minutes, seconds, result='';

            totalSeconds = input / 1000;
            totalMinutes = totalSeconds / 60;
            totalHours = totalMinutes / 60;

            seconds = Math.floor(totalSeconds) % 60;
            minutes = Math.floor(totalMinutes) % 60;
            hours = Math.floor(totalHours) % 60;

            if (hours !== 0) {
                result += hours+':';

                if (minutes.toString().length == 1) {
                    minutes = '0'+minutes;
                }
            }

            result += minutes+':';

            if (seconds.toString().length == 1) {
                seconds = '0'+seconds;
            }

            result += seconds;

            return result;
        };
      });
    }

我认为错误在于ts文件,因为我是角色新手。

使用管道是否有直接转换而不使用函数?

4 个答案:

答案 0 :(得分:1)

其他答案看起来很复杂,终于找到了解决方案。

Html as,

<md-cell *mdCellDef="let row"> {{getFormathours(row?.duration)}} </md-cell>

和ts,

getFormathours(input) {
    var totalHours, totalMinutes, totalSeconds, hours, minutes, seconds, result='';
    totalSeconds = input / 1000;
    totalMinutes = totalSeconds / 60;
    totalHours = totalMinutes / 60;

    seconds = Math.floor(totalSeconds) % 60;
    minutes = Math.floor(totalMinutes) % 60;
    hours = Math.floor(totalHours) % 60;

    console.log (hours + ' : '  + minutes + ' : ' + seconds);
    if (hours !== 0) {
        result += hours+' hr:';

        if (minutes.toString().length == 1) {
            minutes = '0'+minutes;
        }
    }

    result += minutes+' min';

      if (seconds.toString().length == 1) {
          seconds = '0'+seconds;
      }

      result += seconds;

    return result;
}

根据我的问题,这给出了我需要的确切输出和更清晰的解决方案。无论如何,我也非常感谢别人的答案。

答案 1 :(得分:0)

您无需重新发明轮子。您可以使用Date对象来获取持续时间。

假设您有2个日期对象,

  • 了解它们之间的区别。由于您已经有毫秒,因此您已经完成了此步骤。
  • 现在创建一个新的日期对象并删除时间值。
  • 现在,当您为其设置新的时间值(差异)时,您拥有所有值。只需使用该功能并获取值并以您的格式显示。

function getTravelDuration(date1, date2) {
  var diff = +date2 - +date1;
  var today = new Date();
  today.setHours(0,0,0,0);
  var arrival = new Date(+today + diff);
  var duration = ['getFullYear', 'getMonth', 'getDate', 'getHours', 'getMinutes', 'getSeconds'].reduce(function(p,c,i,a){
    var value = arrival[c]() - today[c]();
    if(value) {
      p += value;
      p += ((c === 'getFullYear') ? ' Year' : c.replace('get', ' ')) + ' '
    }
    return p;
  }, '');
  console.log(duration);
}

function doubleDigits(str){
  return ('00' + str).slice(-2)
}

getTravelDuration(new Date(2017, 10, 5, 5), new Date(2017, 10, 5, 15, 23));
getTravelDuration(new Date(2017, 10, 5, 5), new Date(2017, 10, 6, 15, 23, 30));

答案 2 :(得分:0)

对我来说,最好的选择(没有错误)是使用时刻库​​,而不是日期。 请检查https://momentjs.com/docs/

var t1 = moment('2017/10/5 15:23');
var t2 = moment('2017/10/5 15:23');

var differenceMinutes = t1.diff(t2, 'minutes');
var differenceMilliseconds = t1.diff(t2, 'minutes');

使用流行且经过测试的库可能会更好。您可以在角度模板中使用:

https://github.com/urish/angular2-moment

示例:

{{startDate | amDifference: endDate :'' }}

答案 3 :(得分:0)

您可以直接使用此代码

export class StoppageComponent implements OnInit {

constructor() {
 }

ngOnInit() {
}


filter('formatDuration', function () {
    return function (input) {
        var result = new Date(input);
var n = (d.getDate().toString()) +'/'+ (d.getMonth().toString())+'/' +(d.getFullYear().toString()) + '  '+ (d.getHours().toString()) +':'+ (d.getHours().toString()) +':'+ (d.getSeconds().toString());

        return result;
    };
  });
}
相关问题