0

I have two dates 1/1/2023:12.00.00 as the start and 3/3/2023:12.00.00

I want to create a list of dates between these two dates with half an hour start/end time slots, for example 1/1/2023:12.00.00 - 1/1/2023:12.30.00 1/1/2023:12.30.00 - 1/1/2023:13.00.00 all the way up to 3/3/2023:12.00.00

Does any one know a nice and simple way to do this in c#?

All thanks in advance

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Calibre2010
  • 3,729
  • 9
  • 25
  • 35
  • 1
    Does this answer your question? [How do I loop through a date range?](https://stackoverflow.com/questions/1847580/how-do-i-loop-through-a-date-range) – sommmen Jun 27 '23 at 12:35

5 Answers5

2

You can make it as functions to manage it as an IEnumerable

public IEnumerable<DateTime> GetDates(DateTime startDate, DateTime endDate, TimeSpan interval)
{
    var currentDateTime = startDate;
    while (currentDateTime < endDate)
    {
        yield return currentDateTime;
        currentDateTime = currentDateTime.Add(interval);
    }
    yield return endDate;
}

And use this

var dates = GetDates(new DateTime(2023, 1, 1), new DateTime(2023, 1, 3), TimeSpan.FromMinutes(30));
Daniel127
  • 23
  • 3
1

I have the following extension methods lying around for this;

/// <summary>
/// Extensions for dealing with <see cref="DateTime"/>
/// </summary>
public static class DateTimeExtensions
{

    #region Range

    private static readonly Func<DateTime, DateTime> DayStep = x => x.AddDays(1);
    private static readonly Func<DateTime, DateTime> MonthStep = x => x.AddMonths(1);
    private static readonly Func<DateTime, DateTime> YearStep = x => x.AddYears(1);

    /// <summary>
    /// Adapted from <see href="https://stackoverflow.com/a/39847791/4122889"/>
    /// <example>
    /// // same result
    /// var fromToday = birthday.RangeFrom(today);
    /// var toBirthday = today.RangeTo(birthday);
    /// </example>
    /// </summary>
    /// <param name="from"></param>
    /// <param name="to"></param>
    /// <param name="step"></param>
    /// <returns></returns>
    public static IEnumerable<DateTime> RangeTo(this DateTime from, DateTime to, Func<DateTime, DateTime>? step = null)
    {
        step ??= DayStep;

        while (from < to)
        {
            yield return from;
            from = step(from);
        }
    }

    public static IEnumerable<DateTime> RangeToMonthly(this DateTime from, DateTime to)
        => RangeTo(from, to, MonthStep);

    public static IEnumerable<DateTime> RangeToYearly(this DateTime from, DateTime to)
        => RangeTo(from, to, YearStep);


    public static IEnumerable<DateTime> RangeFrom(this DateTime to, DateTime from, Func<DateTime, DateTime>? step = null) 
        => from.RangeTo(to, step);

    #endregion
}

Usage;

DateTime.UtcNow.RangeTo(DateTime.UtcNow.AddDays(1), x => x.AddMinutes(30)).ToList()
06/27/2023 12:31:36
06/27/2023 13:01:36
06/27/2023 13:31:36
06/27/2023 14:01:36
06/27/2023 14:31:36
...
sommmen
  • 6,570
  • 2
  • 30
  • 51
0

Keep calling AddMinutes() to obtain the next value:

var dateTimeValues = new List<DateTime>();

var startDate = new DateTime(2023, 1, 1);
var endDate = new DateTime(2023, 1, 3);

var currentDateTime = startDate;
while ((currentDateTime = currentDateTime.AddMinutes(30)) <= endDate) {
    dateTimeValues.Add(currentDateTime);
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

liq and Enumerable approach:


var startDate = new DateTime(2023,1,1, 12,00,00);
var endDate = new DateTime(2023,3,3, 12,00,00);

var dates = Enumerable.Range(0, int.MaxValue)
          .Select(multiplier => startDate.Add(TimeSpan.FromMinutes(30 * multiplier)))
          .TakeWhile(span => span <= endDate);

dates.Dump();

another way - cleaner:

int chunks = (int)(endDate - startDate).TotalMinutes / 30;
    var rez =  Enumerable.Range(0, chunks + 1)
                     .Select(p => startDate.AddMinutes(30 * p));
                     

result would be IEnumerable enter image description here

Power Mouse
  • 727
  • 6
  • 16
0

You can even use a simple for-loop:

TimeSpan interval = TimeSpan.FromMinutes(30);
for (var dt=new DateTime(2023,1,1); dt<new DateTime(2023,3,3); dt = dt.Add(interval))
{
    Console.WriteLine($"{dt:yyyy-MM-dd HH:mm} - {dt.Add(interval):yyyy-MM-dd HH:mm}");
}

You may need to add 1 day to the enddate, if you want to include date ranges on that day.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111