I want to create an hourly object list from a time range.
Is there any function like python pandas.timerange in c#?
pandas.time_range("11:00", "21:30", freq="30min")
I want to create an hourly object list from a time range.
Is there any function like python pandas.timerange in c#?
pandas.time_range("11:00", "21:30", freq="30min")
 
    
    As an option, take advantage of MoreLInq Generate Method:
//variables used in both examples
var start = TimeSpan.FromHours(11);
var end = TimeSpan.FromHours(21).Add(TimeSpan.FromMinutes(30));
MoreEnumerable.Generate(start, span => span.Add(TimeSpan.FromMinutes(30)))
              .TakeWhile(span => span <= end)
"Native" Linq query is a bit uglier:
Enumerable.Range(0, int.MaxValue)
          .Select(multiplier => start.Add(TimeSpan.FromMinutes(30 * multiplier)))
          .TakeWhile(span => span <= end)
both queries produce:
11:00:00 
11:30:00 
12:00:00 
12:30:00 
13:00:00 
13:30:00 
14:00:00 
  ...
21:00:00 
21:30:00 
 
    
    Here's a function that takes a start/end time and # of minutes between:
public IEnumerable<DateTime> GetTimeRange(DateTime startTime, DateTime endTime, int minutesBetween)
{
   int periods = (int)(endTime - startTime).TotalMinutes / minutesBetween;  
   return Enumerable.Range(0,periods+1)
                    .Select (p => startTime.AddMinutes(minutesBetween * p));
}
You can just parse the initial strings when calling the function:
var times = GetTimeRange(DateTime.Parse("11:00"),DateTime.Parse("21:30"),30);
