将timeStamp转换为小时表单的函数

时间:2017-12-18 20:13:29

标签: angular

我已经构建了这个函数,将时间从timeStamp表单转换为小时表单,但结果显示了一个数字的时间.... 例如(420> 7 am)我想表现出像(07 am)

 convertTime(time: number) {

    let hours: number = Math.floor(time / 60);
    let minutes: number = time % 60;

    if (hours <= 12) return hours + ':' + minutes + ' am';
    else return (hours - 12 ) + ':' + minutes + ' pm';
}

1 个答案:

答案 0 :(得分:0)

convertTime(time: number) {

    let hours: number = Math.floor(time / 60);
    let minutes: number = time % 60;

    if (hours <= 12 && hours >=10) return hours + ':' + minutes + ' am';
    else if(hours < 10) return '0' + hours + ':'+ minutes +' am';

    else return (hours - 12 ) + ':' + minutes + ' pm';
}