简体   繁体   English

在Razor View中将小时的整数值转换为格式化的小时

[英]Convert integer value of hours into formatted hours in Razor View

I'm trying to convert my integer value of hours, 0-23 into a nicely formatted one like: 我正在尝试将0-23的整数小时转换为格式良好的格式,例如:

15 would be represented like: 15:00
0 would be represetnted like: 00:00
23 like: 23:00

This is the code from my view: 这是我认为的代码:

 @foreach (var item in ViewBag.HourlyGraph)
 {
   @:{device: '@item.Hour', geekbench:@item.Sales },
 }

I'm trying to represent the @item.Hour value like I mentioned above. 我正在尝试代表如上所述的@ item.Hour值。 I've tried using interpolated string like this: 我试过使用插值的字符串,像这样:

"{@item.Hour}:00"

But in my view says it's not supported since I use C# 5.0 instead of C# 6.0 ... 但是我认为它不受支持,因为我使用C#5.0而不是C#6.0 ...

How could do this instead of using interpolated strings? 怎么做而不是使用插值字符串?

您可以对整数使用标准的字符串格式,如果需要,该格式将以零开头:

@item.Hour.ToString("D2")

Can you just use the ToString method? 您可以只使用ToString方法吗?

 @foreach (var item in ViewBag.HourlyGraph)
 {
   @:{device: '@item.Hour.ToString("00")<text>:00</text>', geekbench:@item.Sales },
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM