.net 如何创建一个日期数组,其中两个日期之间的开始/结束时间为30分钟?

oewdyzsn  于 2023-07-01  发布在  .NET
关注(0)|答案(5)|浏览(139)

我有两个日期1/1/2023:12.00.00作为开始和3/3/2023:12.00.00
我想创建一个介于这两个日期之间的日期列表,开始/结束时间间隔为半小时,例如1/1/2023:12.00.00 - 1/1/2023:12.30.00 1/1/2023:12.30.00 - 1/1/2023:13.00.00一直到3/3/2023:12.00.00
有没有人知道一个很好的和简单的方法在C#中做到这一点?
先谢谢你

0qx6xfy6

0qx6xfy61#

您可以将其设置为函数,以便将其作为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;
}

用这个

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

mm9b1k5b2#

我有下面的扩展方法来实现这一点;

/// <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
}

用途;

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
...
rslzwgfq

rslzwgfq3#

继续调用AddMinutes()以获取下一个值:

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);
}
evrscar2

evrscar24#

liq和Enumerable方法:

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();

另一种方式-更清洁:

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

结果将是IEnumerable x1c 0d1x

qxsslcnc

qxsslcnc5#

你甚至可以使用一个简单的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}");
}

如果要在结束日期上包括日期范围,则可能需要在该日期上添加1天。

相关问题