Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查时间跨度内发生的日期和月份_C#_Datetime - Fatal编程技术网

C# 检查时间跨度内发生的日期和月份

C# 检查时间跨度内发生的日期和月份,c#,datetime,C#,Datetime,我有一个列表dateHolder,它具有DateTime数据类型并保存数百个DateTime值。列表从最早的值累加到最新的值。这是一个函数: private void CheckQuarter() { int lastPoint = dateHolder.Count(); //Has dates from 4/12/2015 to 30/11/2017 DateTime startDate = dateHolder(0); //startDate = 4/12/2015

我有一个列表
dateHolder
,它具有DateTime数据类型并保存数百个DateTime值。列表从最早的值累加到最新的值。这是一个函数:

private void CheckQuarter()
{
    int lastPoint = dateHolder.Count(); //Has dates from 4/12/2015 to 30/11/2017
    DateTime startDate = dateHolder(0); //startDate = 4/12/2015
    DateTime endDate = dateHolder(lastPoint - 1); //endDate = 30/11/2017
}
从上面的代码示例中,我只想根据日期和月份检查某些日期的出现情况。我对年值不感兴趣。我想检查的日期和月份值是:

1st Jan
31st Mar
1st Apr
30th Jun
1st Jul
30th Sep
1st Oct
31st Dec

从上面的日期和月份值中,您可以看到我正在检查季度值。现在,在
开始日期
结束日期
中,如何检查我提到的那些日期的出现情况?非常感谢

LINQ
Enumerable.Count
有您可以使用的。e、 g.如果你只关心月份和日期:

var yourTargetDates = new List<DateTime>()
{
    new DateTime(1, 1, 1), // Jan 1st
    new DateTime(1, 3, 31), // March 31st
    ... // etc
};

var occurrences = dateHolder.Count(d => yourTargetDates.Any(td => td.Month == d.Month && td.Day == d.Day));
var yourTargetDates=new List()
{
新日期时间(1,1,1),//1月1日
新日期时间(1,3,31),//3月31日
…//等等
};
var事件=dateHolder.Count(d=>yourTargetDates.Any(td=>td.Month==d.Month&&td.Day==d.Day));

你可能仍然需要迎合某些日期和时间的怪异,尽管处理日期和时间时通常会遇到这种情况。

我找到了一种方法。这就是功能:

private void CheckQuarter()
{
    int lastPoint = dateHolder.Count();
    DateTime startDate = dateHolder(0);
    DateTime endDate = dateHolder(lastPoint - 1);
    DateTime CurrD = startDate;
    while ((CurrD <= endDate))
    {
        if ((CurrD.Month == 1 && CurrD.Day == 1))  //1st Jan
        {
            //Do your process
        }
        else if ((CurrD.Month == 3 && CurrD.Day == 31))  //31st Mar
        {
            //Do your process
        }
        else if ((CurrD.Month == 4 && CurrD.Day == 1))  //1st Apr
        {
            //Do your process
        }
        else if ((CurrD.Month == 6 && CurrD.Day == 30))  //30th Jun
        {
            //Do your process
        }
        else if ((CurrD.Month == 7 && CurrD.Day == 1))  //1st July
        {
            //Do your process
        }
        else if ((CurrD.Month == 9 && CurrD.Day == 30))  //30th Sep
        {
            //Do your process
        }
        else if ((CurrD.Month == 10 && CurrD.Day == 1))  //1st Oct
        {
            //Do your process
        }
        else if ((CurrD.Month == 12 && CurrD.Day == 31))  //31st Dec
        {
            //Do your process
        }
        else
        {
           //Do your process
        }

        CurrD = CurrD.AddDays(1);
    }
}
private void CheckQuarter()
{
int lastPoint=dateHolder.Count();
DateTime startDate=日期持有者(0);
DateTime endDate=日期持有者(lastPoint-1);
DateTime CurrD=开始日期;

当((CurrD我理解你的观点,但我相信“var事件”将返回“var yourTargetDates”列表的计数。但是,如果“dateHolder”不包含日期怎么办?我需要的是在“startDate”和“endDate”的时间跨度之间,这些日期的事件。“dateHolder”可能不包含这些日期values@Hari
事件
将简单地说就是出现的次数。如果没有满足谓词要求的谓词,那么它将是
0
。您可以将谓词更改为您想要的任何其他条件。“检查出现次数”是什么意思意思是?@NetMage从'startDate'到'endDate',我想看看我提到的8个日期是否发生?你的意思是这8个日期是否发生在
日期持有者
内,还是这8个日期发生在
startDate
endDate
之间?你想要全部8个日期,还是想知道哪一个日期发生在startDate和EndDate之间“*dates”在英语中是什么意思?