net mvc应用中的Timespan

时间:2022-07-18 04:13:46

I'm writing an asp.net mvc app. in c#, and I'm wondering if anybody can help me to understand, if it's possible to display the time like 8:00 (without seconds) on the form in a dropdown list instead of 8:00:00. Thanks for your help!

我正在用c#编写一个asp.net mvc应用程序,我想知道是否有人能帮助我理解,是否有可能在一个下拉列表中显示像8:00(没有秒)这样的时间而不是8:00:00。谢谢你的帮助!

Right now I have a code in my timehepler.cs:

现在我有一个代码在我的时间表。

public static List<TimeSpan> TimeSpansInRange(TimeSpan start, TimeSpan end, TimeSpan interval)
        {
            List<TimeSpan> timeSpans = new List<TimeSpan>();
            while (start.Add(interval) <= end)
            {
                timeSpans.Add(start);
                start = start.Add(interval);
            }
            return timeSpans;
        }

        public static List<TimeSpan> PossibleTimeSpansInDay()
        {
            return TimeSpansInRange(new TimeSpan(8, 0, 0), new TimeSpan(22, 30, 0), new TimeSpan(0, 30, 0));
        }

2 个解决方案

#1


1  

You have to change the List Type to String and then while adding convert it to the format you required unfortunately there is no alternative of it except converting the result to string as much i know....

你必须改变类型的字符串列表,然后在添加它转换为所需的格式你不幸的是没有选择除了我知道....一样将结果转换为字符串

   public static List<string> TimeSpansInRange(TimeSpan start, TimeSpan end, TimeSpan interval)
    {
        List<string> timeSpans = new List<string>();
        while (start.Add(interval) <= end)
        {
            timeSpans.Add(start.Hours.ToString() +":"+start.Minutes.ToString());
            start = start.Add(interval);
        }
        return timeSpans;
    }

#2


1  

You can't just use ToString like the control is probably doing.

不能像控件那样使用ToString。

Instead, convert it to a string yourself with

相反,你可以自己把它转换成字符串

public static class TimeSpanExtensions
{
   private static string ConvertTimeSpanToString (this TimeSpan myTimeSpan)
   {
      return (int) myTimeSpan.TotalHours + ":" + myTimeSpan.Minutes.ToString("00");
   }
}

You'll call it this way:

你可以这样称呼它:

<%= Html.DropDownList("Start", Model.Request.StartTimes.Select(TimeSpanExtensions.ConvertTimeSpanToString) %> 
<%= Html.DropDownList("End", Model.Request.EndTimes.Select(TimeSpanExtensions.ConvertTimeSpanToString) %>

In .net 4, you could probably just use a format string in your listbox. myTimeSpan.ToString ("HH:mm"); when you display the time.

在。net 4中,您可以在列表框中使用格式字符串。myTimeSpan。ToString(“HH:mm”);显示时间。

#1


1  

You have to change the List Type to String and then while adding convert it to the format you required unfortunately there is no alternative of it except converting the result to string as much i know....

你必须改变类型的字符串列表,然后在添加它转换为所需的格式你不幸的是没有选择除了我知道....一样将结果转换为字符串

   public static List<string> TimeSpansInRange(TimeSpan start, TimeSpan end, TimeSpan interval)
    {
        List<string> timeSpans = new List<string>();
        while (start.Add(interval) <= end)
        {
            timeSpans.Add(start.Hours.ToString() +":"+start.Minutes.ToString());
            start = start.Add(interval);
        }
        return timeSpans;
    }

#2


1  

You can't just use ToString like the control is probably doing.

不能像控件那样使用ToString。

Instead, convert it to a string yourself with

相反,你可以自己把它转换成字符串

public static class TimeSpanExtensions
{
   private static string ConvertTimeSpanToString (this TimeSpan myTimeSpan)
   {
      return (int) myTimeSpan.TotalHours + ":" + myTimeSpan.Minutes.ToString("00");
   }
}

You'll call it this way:

你可以这样称呼它:

<%= Html.DropDownList("Start", Model.Request.StartTimes.Select(TimeSpanExtensions.ConvertTimeSpanToString) %> 
<%= Html.DropDownList("End", Model.Request.EndTimes.Select(TimeSpanExtensions.ConvertTimeSpanToString) %>

In .net 4, you could probably just use a format string in your listbox. myTimeSpan.ToString ("HH:mm"); when you display the time.

在。net 4中,您可以在列表框中使用格式字符串。myTimeSpan。ToString(“HH:mm”);显示时间。