Sprache: C#
Diese Methode gibt eine Liste von Uhrzeiten in diesem Format zurück:
"00:00"
"00:30"
"01:00"
"01:30"
"02:00"
"02:30"
:
usw.
Diese Liste kann an eine ComboBox für eine Zeitauswahl gebunden werden.
/// <summary>
/// Gets the time strings.
/// </summary>
/// <returns></returns>
static List<string> GetTimeStrings()
{
var list = new List<string>();
var ts = new TimeSpan(0, 0, 0, 0);
while (ts.Days < 1)
{
list.Add(string.Format("{0}:{1}", ts.Hours.ToString().PadLeft(2, '0'), ts.Minutes.ToString().PadRight(2, '0')));
ts = ts.Add(new TimeSpan(0, 0, 30, 0));
}
return list;
}
/// <summary>
/// Gets the time strings.
/// </summary>
/// <returns></returns>
static List<string> GetTimeStrings()
{
var list = new List<string>();
var ts = new TimeSpan(0, 0, 0, 0);
while (ts.Days < 1)
{
list.Add(string.Format("{0}:{1}", ts.Hours.ToString().PadLeft(2, '0'), ts.Minutes.ToString().PadRight(2, '0')));
ts = ts.Add(new TimeSpan(0, 0, 30, 0));
}
return list;
}
Alte URL:
/snippet/formatierte-uhrzeiten-fuer-die-zeitauswahl-erzeugen/903
[code] GetTimeList(Int32 minutesInterval) retVal;
private static IList
{
List
DateTime dt;
dt = new DateTime(1, 1, 1, 0, 0, 0);(24 * 60 / minutesInterval);
retVal = new List
while(dt.Day < 2) { retVal.Add(dt.ToString("HH:mm")); dt = dt.AddMinutes(minutesInterval); } return (retVal); } [/code]
Super, das ist noch ausfühlicher. Für meinen Fall hatte die einfache Version gereicht. Danke!
Jan