在 C# 中的 Custom TimePicker 中声明时间变量的最佳方法是什么
What is the best way to declare a time variable in C# in Custom TimePicker
现在我正在寻找一个免费可靠的 TimePicker 控件以在我的 MVC 应用程序中使用,直到我厌倦了找到一个并决定通过 C# 制作我自己的使用..
我这样做了并且效果很好(可以随意复制或重复使用..):
public static class OpeningTimesList
{
/// <summary>
/// Provides a listItems of opening hours to use in an HTML DropDownListFor helper.
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static List<SelectListItem> GetOpeningHours(string time)
{
var selectOptions = new string[]
{
"07:00",
"07:30",
"08:00",
"08:30",
"09:00",
"09:30",
"10:00",
"10:30",
"11:00",
"11:30",
"12:00",
"12:30",
"13:00",
"13:30",
"14:00",
"14:30",
"15:00",
"15:30",
"16:00",
"16:30",
"17:00",
"17:30",
"18:00",
"18:30",
"19:00",
"19:30",
"20:00",
"20:30",
"21:00",
"21:30",
"22:00",
"22:30"
};
return GetDropDownList(selectOptions, time);
}
/// <summary>
/// Gets List Items in Text only, instead of a combination of text and value.
/// </summary>
/// <param name="selectOptions"></param>
/// <param name="selectedOption"></param>
/// <returns></returns>
private static List<SelectListItem> GetDropDownList(string[] selectOptions, string selectedOption)
{
var listItems = new List<SelectListItem>();
foreach (var option in selectOptions)
{
listItems.Add(new SelectListItem
{
Text = option,
Selected = string.Compare(option, selectedOption, StringComparison.CurrentCultureIgnoreCase) == 0
});
}
return listItems;
}
}
MVC 剃须刀中的使用年龄:
@Html.DropDownListFor(x => x.StartFrom, Website.ViewModels.ViewHelpers.OpeningTimesList.GetOpeningHours(Model.StartFrom.ToString()))
我的问题是我的 StartFrom
属性 不是 string
;它是一个 TimeSpan
(或者它应该是一个 DateTime
。我可以转换 ToString()
或在后面的代码中进行转换,但理想情况下我不应该这样做。使用String
时间项目很简单 - 但我应该使用什么来让它变得更好;我是使用 DateTime
还是使用 TimeSpan
.
所以在一行中:您将使用什么来表示时间值,例如 - 例如:08:30。您会使用 TimeSpan
还是 DateTime
?
非常感谢。
都没有。 DateTime 表示日期和时间,TimeSpan 表示某个 length 的时间。您的变量代表可以在任何一天发生的特定重复时刻。
所以尽管不得不妥协,use the one that best fits your use case - or take a look at NodaTime's Period。
当然这取决于你想对结果做什么。您可以只使用 TimeSpan
并将其添加到某个 DateTime.Date
以将 TimeSpam 中的小时和分钟添加到 Date 的日期。
现在我正在寻找一个免费可靠的 TimePicker 控件以在我的 MVC 应用程序中使用,直到我厌倦了找到一个并决定通过 C# 制作我自己的使用..
我这样做了并且效果很好(可以随意复制或重复使用..):
public static class OpeningTimesList
{
/// <summary>
/// Provides a listItems of opening hours to use in an HTML DropDownListFor helper.
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static List<SelectListItem> GetOpeningHours(string time)
{
var selectOptions = new string[]
{
"07:00",
"07:30",
"08:00",
"08:30",
"09:00",
"09:30",
"10:00",
"10:30",
"11:00",
"11:30",
"12:00",
"12:30",
"13:00",
"13:30",
"14:00",
"14:30",
"15:00",
"15:30",
"16:00",
"16:30",
"17:00",
"17:30",
"18:00",
"18:30",
"19:00",
"19:30",
"20:00",
"20:30",
"21:00",
"21:30",
"22:00",
"22:30"
};
return GetDropDownList(selectOptions, time);
}
/// <summary>
/// Gets List Items in Text only, instead of a combination of text and value.
/// </summary>
/// <param name="selectOptions"></param>
/// <param name="selectedOption"></param>
/// <returns></returns>
private static List<SelectListItem> GetDropDownList(string[] selectOptions, string selectedOption)
{
var listItems = new List<SelectListItem>();
foreach (var option in selectOptions)
{
listItems.Add(new SelectListItem
{
Text = option,
Selected = string.Compare(option, selectedOption, StringComparison.CurrentCultureIgnoreCase) == 0
});
}
return listItems;
}
}
MVC 剃须刀中的使用年龄:
@Html.DropDownListFor(x => x.StartFrom, Website.ViewModels.ViewHelpers.OpeningTimesList.GetOpeningHours(Model.StartFrom.ToString()))
我的问题是我的 StartFrom
属性 不是 string
;它是一个 TimeSpan
(或者它应该是一个 DateTime
。我可以转换 ToString()
或在后面的代码中进行转换,但理想情况下我不应该这样做。使用String
时间项目很简单 - 但我应该使用什么来让它变得更好;我是使用 DateTime
还是使用 TimeSpan
.
所以在一行中:您将使用什么来表示时间值,例如 - 例如:08:30。您会使用 TimeSpan
还是 DateTime
?
非常感谢。
都没有。 DateTime 表示日期和时间,TimeSpan 表示某个 length 的时间。您的变量代表可以在任何一天发生的特定重复时刻。
所以尽管不得不妥协,use the one that best fits your use case - or take a look at NodaTime's Period。
当然这取决于你想对结果做什么。您可以只使用 TimeSpan
并将其添加到某个 DateTime.Date
以将 TimeSpam 中的小时和分钟添加到 Date 的日期。