我有一个需要花费几分钟时间的对象,例如整数。
有没有办法将此int数字(分钟)传递为日期格式?
我尝试使用有角日期管道,但转换错误。
有人可以帮我吗?
代码
<div *ngFor="let m of data">
<p>{{m.minutes * 1000 | date:'HH:mm'}}</p>
</div>
答案 0 :(得分:5)
将您的分钟转换为毫秒。
<div *ngFor="let m of data">
<p>{{m.minutes * 1000 * 60| date:'HH:mm':'UTC'}}</p>
</div>
根据documentation for the pipe´s value:
日期表达式:日期对象,数字(自UTC时代起毫秒)或ISO字符串(https://www.w3.org/TR/NOTE-datetime)。
答案 1 :(得分:1)
尝试一下,您可以根据需要传递动态值
function timeConvert(n:number) {
var num = n;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
return num + " minutes = " + rhours + " hour(s) and " + rminutes + " minute(s).";
}
console.log(timeConvert(200));
答案 2 :(得分:0)
也许是这样吗?
pad(m.minutes/60) + ":" + pad(m.minutes%60)
具有填充功能:
function pad(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
PS:它不会成为日期对象,但会像您的示例中那样显示时间:)
PS2:您可以将其更改为:
pad((m.minutes/60).toFixed(0)) + ":" + pad(m.minutes%60)
如果存在问题,请删除多余的小数
答案 3 :(得分:0)
您可以通过如下更改代码来实现。 app.component.html
<div *ngFor="let m of data">
<p>{{ math.floor(m.minutes / 60) | number: '2.'}}:{{m.minutes % 60 | number: '2.'}}</p>
</div>
app.component.ts
export class AppComponent {
data=[ your data here ]
math = Math;
}
我希望这会有所帮助。:)