本文介绍了如何在angular2中将秒转换为时间字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我一直在寻找整个网络中的此功能,但没有找到可以用来将秒转换为可以表示为字符串的年,月,日,小时,分钟和秒的解决方案。
So I've been looking for this functionality throughout the net and haven't found a solution that I could use to convert seconds to years, months, days, hours, minutes and seconds that could be represented as a string.
推荐答案
我想出了Angular2中Pipe的解决方案,但是我想对可能是
I have came up with solution of a Pipe in Angular2, however I would like to get some feedback on things that could be done better to improve it.
此外,也许其他一些人将需要这种管道,所以我就把它留在这里分享。
Moreover maybe some other people will be in a need of this kind of pipe, so I'm just leaving it here to share.
import {Pipe} from "angular2/core";
@Pipe({
name: 'secondsToTime'
})
export class secondsToTimePipe{
times = {
year: 31557600,
month: 2629746,
day: 86400,
hour: 3600,
minute: 60,
second: 1
}
transform(seconds){
let time_string: string = '';
let plural: string = '';
for(var key in this.times){
if(Math.floor(seconds / this.times[key]) > 0){
if(Math.floor(seconds / this.times[key]) >1 ){
plural = 's';
}
else{
plural = '';
}
time_string += Math.floor(seconds / this.times[key]).toString() + ' ' + key.toString() + plural + ' ';
seconds = seconds - this.times[key] * Math.floor(seconds / this.times[key]);
}
}
return time_string;
}
}
这篇关于如何在angular2中将秒转换为时间字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!