C# 获取两个'之间的所有日期时间;日期时间';C中的s#

C# 获取两个'之间的所有日期时间;日期时间';C中的s#,c#,datetime,C#,Datetime,我有两个DateTimes,我想得到这些日期之间的所有DateTimes。例如,如果我的日期类似于2010年1月1日-2010年1月5日,则我的函数应返回日期列表(list),并且它必须包含2010年1月1日、2010年1月2日、2010年1月3日、2010年1月4日和2010年1月5日 我写了一个这样的函数。如果我的约会是在一个月内,那就行了。如果我的日期是2010年1月1日-2010年2月5日,那么它将不起作用。因为月份变了,我的功能无法处理。C#中是否有函数返回两个日期之间的所有日期?或者

我有两个
DateTime
s,我想得到这些日期之间的所有
DateTime
s。例如,如果我的日期类似于2010年1月1日-2010年1月5日,则我的函数应返回日期列表(list),并且它必须包含2010年1月1日、2010年1月2日、2010年1月3日、2010年1月4日和2010年1月5日

我写了一个这样的函数。如果我的约会是在一个月内,那就行了。如果我的日期是2010年1月1日-2010年2月5日,那么它将不起作用。因为月份变了,我的功能无法处理。C#中是否有函数返回两个日期之间的所有日期?或者我如何处理月份的变化

public void GetAllDatesAndInitializeTickets(DateTime startingDate, DateTime endingDate)
    {
        List<DateTime> allDates = new List<DateTime>();

        int starting = startingDate.Day;
        int ending = endingDate.Day;

        for (int i = starting; i <= ending; i++)
        {
            allDates.Add(new DateTime(startingDate.Year, startingDate.Month, i));
        }
public void GetAllDates和Initializetickets(日期时间开始日期、日期时间结束日期)
{
List allDates=新列表();
int starting=startingDate.Day;
int end=endingDate.Day;

对于(int i=start;i您可以直接在循环中使用
DateTime
对象来代替
int
DateTime。AddDays
正确处理月末

for (DateTime date = startingDate; date <= endingDate; date = date.AddDays(1))
    allDates.Add(date);
for(DateTime date=startingDate;date
public IEnumerable GetAllDates和Initializetickets(DateTime startingDate,DateTime endingDate)
{
如果(结束日期<开始日期)
{
抛出新ArgumentException(“endingDate应在startingDate之后”);
}
var ts=结束日期-开始日期;
对于(int i=0;i
你离得太近了……不要用这一天,用整个日期

static IEnumerable<DateTime> GetAllDatesAndInitializeTickets(DateTime startingDate, DateTime endingDate)
{
    List<DateTime> allDates = new List<DateTime>();


    for (DateTime i = startingDate; i <= endingDate; i = i.AddDays(1))
    {
        allDates.Add(i);
    }
    return allDates.AsReadOnly();
}
静态IEnumerable GetAllDates和Initializetickets(日期时间开始日期、日期时间结束日期)
{
List allDates=新列表();

对于(DateTime i=startingDate;i这样的东西怎么样

public IEnumerable<DateTime> DateRange(DateTime fromDate, DateTime toDate)
{
    return Enumerable.Range(0, toDate.Subtract(fromDate).Days + 1)
                     .Select(d => fromDate.AddDays(d));
}
public IEnumerable DateRange(DateTime fromDate,DateTime toDate)
{
返回可枚举范围(0,toDate.Subtract(fromDate).Days+1)
.选择(d=>fromDate.AddDays(d));
}

编辑:立即测试。

根据您的起始代码,并使用编写本文时可用的功能,这里有一个快速控制台应用程序来演示如何操作-使用
AddDays()

class Program
{
    static void Main(string[] args)
    {
        GetDates(new DateTime(2010, 1, 1), new DateTime(2010, 2, 5));

        Console.ReadKey();
    }

    static List<DateTime> GetDates(DateTime startDate, DateTime endDate)
    {
        List<DateTime> dates = new List<DateTime>();

        while ((startDate = startDate.AddDays(1)) < endDate)
            dates.Add(startDate);

        return dates;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
GetDates(新日期时间(2010,1,1),新日期时间(2010,2,5));
Console.ReadKey();
}
静态列表GetDates(DateTime startDate、DateTime endDate)
{
列表日期=新列表();
而((startDate=startDate.AddDays(1))

尽管我认为是一个更好的解决方案。

给定字符串中较低的日期值和较高的日期值,并将频率作为第三个参数,此方法应返回日期字典;其中键是日期范围的起始值,值是相应的范围。 如果频率是每周或每月一次,这很好-您可以根据需要定制。 传递的日期值应采用正确的格式,或者您可能需要使用tryParseExact或类似的格式

    protected static Dictionary<DateTime, String> getDateRange(String lowerDate, String higherDate, String frequency)
    {
        DateTime startDate, endDate;
        startDate = Convert.ToDateTime(lowerDate);
        endDate = Convert.ToDateTime(higherDate);

        Dictionary<DateTime, String> returnDict = new Dictionary<DateTime, String>();

        while (frequency.Equals("weekly") ? (startDate.AddDays(7) <= endDate) : (startDate.AddMonths(1) <= endDate))
        {
            if (frequency.Equals("weekly"))
            {
                returnDict.Add(startDate, startDate + "-" + startDate.AddDays(7));
                startDate = startDate.AddDays(8);
            }
            if (frequency.Equals("monthly"))
            {
                returnDict.Add(startDate, startDate + "-" + startDate.AddMonths(1));
                startDate = startDate.AddMonths(1).AddDays(1);
            }
        }

        returnDict.Add(startDate, startDate + "-" + endDate);

        return returnDict;
    }
受保护的静态字典getDateRange(字符串下限、字符串上限、字符串频率)
{
日期时间开始日期,结束日期;
startDate=转换为ToDateTime(lowerDate);
endDate=Convert.ToDateTime(higherDate);
Dictionary returnDict=新字典();

while(frequency.Equals(“weekly”)?(startDate.AddDays(7)如果日期包含不同的小时,则顶级解决方案将失败。下面是一个获取所有小时和所有天的解决方案:

所有日期:

static public List<string> get_days_between_two_dates(DateTime start_date, DateTime end_date)
    {
        List<string> days_list = new List<string>();
        DateTime temp_start;
        DateTime temp_end;

        //--Normalize dates by getting rid of minues since they will get in the way when doing the loop
        temp_start = new DateTime(start_date.Year, start_date.Month, start_date.Day);
        temp_end = new DateTime(end_date.Year, end_date.Month, end_date.Day);

        //--Example Should return
        //--1-12-2014 5:59AM - 1-13-2014 6:01AM return 12 and 13
        for (DateTime date = temp_start; date <= temp_end; date = date.AddDays(1))
        {
            days_list.Add(date.ToShortDateString());
        }

        return days_list;
    }
静态公共列表获取两个日期之间的日期(日期时间开始日期、日期时间结束日期)
{
列表天数\列表=新列表();
开始日期时间;
日期时间临时结束;
//--通过去掉分钟来规范化日期,因为在循环时它们会妨碍时间
临时开始=新的日期时间(开始日期.年,开始日期.月,开始日期.日);
temp_end=新日期时间(end_date.Year、end_date.Month、end_date.Day);
//--示例应该返回
//--2014年12月1日上午5:59-2014年13月1日上午6:01返回12和13
对于(DateTime date=temp_start;date
static IEnumerable GetAllDate和Initializetickets)(DateTime startingDate,DateTime endingDate)
{
List allDates=新列表();

对于(DateTime i=startingDate;我很好地使用了yield,应该考虑到这一点。请格式化代码,使函数的第一行也被格式化为代码。这个答案并不能解决问题,因为他写道他已经有两个DateTime并且想要一个列表,但是你的函数使用字符串而不是DateTimes并返回一个字典而不是一张清单。而且,他想要开始日期和结束日期之间的每个日期,而不仅仅是一周一次或一个月一次。这正是我需要的。有效而且非常干净。
static public List<string> get_days_between_two_dates(DateTime start_date, DateTime end_date)
    {
        List<string> days_list = new List<string>();
        DateTime temp_start;
        DateTime temp_end;

        //--Normalize dates by getting rid of minues since they will get in the way when doing the loop
        temp_start = new DateTime(start_date.Year, start_date.Month, start_date.Day);
        temp_end = new DateTime(end_date.Year, end_date.Month, end_date.Day);

        //--Example Should return
        //--1-12-2014 5:59AM - 1-13-2014 6:01AM return 12 and 13
        for (DateTime date = temp_start; date <= temp_end; date = date.AddDays(1))
        {
            days_list.Add(date.ToShortDateString());
        }

        return days_list;
    }
static public List<string> get_hours_between_two_dates(DateTime start_date, DateTime end_date)
    {
        List<string> hours_24_list = new List<string>();
        DateTime temp_start;
        DateTime temp_end;

        //--Normalize dates by getting rid of minutes since they will get in the way when doing the loop
        temp_start = new DateTime(start_date.Year, start_date.Month, start_date.Day, start_date.Hour, 0, 0);
        temp_end = new DateTime(end_date.Year, end_date.Month, end_date.Day, end_date.Hour, 0, 0);

        //--Example Should return
        //--5:59AM - 6:01AM return 5am and 6am
        for (DateTime date = temp_start; date <= temp_end; date = date.AddHours(1))
        {
            hours_24_list.Add(date.ToShortTimeString());
        }

        return hours_24_list;
    }
static IEnumerable<DateTime> GetAllDatesAndInitializeTickets(DateTime startingDate, DateTime endingDate)
{
    List<DateTime> allDates = new List<DateTime>();


    for (DateTime i = startingDate; i <= endingDate; i = i.AddDays(1))
    {
        allDates.Add(i);
    }
    return allDates.AsReadOnly();
}