如何在 30 分钟内显示 TimeSpan

How to display TimeSpan on 30 minute

我想在 30 分钟循环时显示 TimeSpan,并且只显示如下:

00:00

00:30

01:00

01:30

这是我的代码:

<select class="form-control dropdown guestTitle">
 @{
   
  TimeSpan StartTime = TimeSpan.FromHours(0);
  <option>00:00</option>
  for (int i = 0; i < 24; i++)
  {
   StartTime = StartTime.Add(new TimeSpan(00,30,0));
   <option>@StartTime</option>
  }                      
 }                                                                                           
</select>

显示如下:

 00:00

 00:30:00

 01:00:00

 01:30:00

我对这种格式的问题是显示的秒数。另外我希望时间是 24 小时格式而不是 12 小时格式。

你可以给它一个格式ToString:

    <select class="form-control dropdown guestTitle">
    @{
       TimeSpan StartTime = TimeSpan.FromHours(0);
       <option>00:00</option>
       for (int i = 0; i < 48; i++)
           {
               StartTime = StartTime.Add(new TimeSpan(00,30,0));
               <option>@StartTime.ToString("hh\:mm")</option>
           }                      
      }                                                                                           
  </select>