Reputation: 325
I have an object that takes several minutes, like int numbers.
Is there a way to pass this int number (minutes) into the date format?
I tried to use the angular date pipe but the conversion is wrong.
Can someone help me?
code
<div *ngFor="let m of data">
<p>{{m.minutes * 1000 | date:'HH:mm'}}</p>
</div>
Upvotes: 1
Views: 4835
Reputation: 1653
maybe something like this?
pad(m.minutes/60) + ":" + pad(m.minutes%60)
with the padding function:
function pad(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
PS: it's not going to be a date object, but it will display the time as in your examples :)
PS2: you can change it to:
pad((m.minutes/60).toFixed(0)) + ":" + pad(m.minutes%60)
to get rid of the extra decimals if this was the problem
Upvotes: 0
Reputation: 41
You can do this by changing your code as follows. 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;
}
I hope this helps.:)
Upvotes: 1
Reputation: 820
Try this , You can pass dynamic values to your need
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));
Upvotes: 2
Reputation: 414
Convert your minutes to milliseconds.
<div *ngFor="let m of data">
<p>{{m.minutes * 1000 * 60| date:'HH:mm':'UTC'}}</p>
</div>
According to the documentation for the pipe´s value:
The date expression: a Date object, a number (milliseconds since UTC epoch), or an ISO string (https://www.w3.org/TR/NOTE-datetime).
Upvotes: 7