我正在构建一个使用Angular + Typescript的web应用程序。我有ASP.NET核心Web API,我尝试以这种格式10:10:10显示前端时间,但它以这种格式显示
{
"Hours":16,
"Minutes":8,
"Seconds":45,
"Milliseconds":0,
"Ticks":581250000000,
"Days":0,
"TotalDays":0.6727430555555556,
"TotalHours":16.145833333333332,
"TotalMilliseconds":58125000,
"TotalMinutes":968.75,
"TotalSeconds":58125
}如何在typescript中显示timespan?
我的html代码
<ng-container matColumnDef="startday">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{'::Startday' | abpLocalization}}
</th>
<pre mat-cell *matCellDef="let element">
{{ element.startday| json}}
</pre>
</ng-container>发布于 2021-01-06 19:43:07
您可以简单地从对象中使用小时、分钟、秒并将其连接到一个字符串中
此外,我还添加了将一个数字转换为01格式的方法getString(),如果是12,它将是相同的12
我们得到了01:01:01,而不是1:1:1
const time = {
"Hours":16,
"Minutes":8,
"Seconds":45,
"Milliseconds":0,
"Ticks":581250000000,
"Days":0,
"TotalDays":0.6727430555555556,
"TotalHours":16.145833333333332,
"TotalMilliseconds":58125000,
"TotalMinutes":968.75,
"TotalSeconds":58125
};
function getString(number) {
return number.toString().padStart(2, "0") // converts 2 to 02, 7 to 07
}
const formattedTime = `${getString(time.Hours)}:${getString(time.Minutes)}:${getString(time.Seconds)}`;
console.log(formattedTime)
https://stackoverflow.com/questions/65594602
复制相似问题