如何在给定的日期范围内从给定的工作日获取日期 [英] how to get dates from given weekdays in a given daterange

查看:85
本文介绍了如何在给定的日期范围内从给定的工作日获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在给定日期范围内从给定工作日获取日期



我必须提供选项



1.选择日期范围

例如:

01-01-2015至01-03-2015



2.从组合框中选择工作日

例如周日,星期一等



3.选择DayType,如Ist,second,third from checkboxlist



如果我选择星期日并在第一天和最后一天打字,那么我想得到第一天和最后一个星期日的日期(格式为dd / MM / yyyy)日期范围。



怎么可能



请帮助我



提前谢谢......

How to get dates from given weekdays in a given daterange

I have to give options for

1. selecting Date Range
eg:
01-01-2015 to 01-03-2015

2. Selecting weekday from combobox
example sunday,Monday etc

3. Selecting DayType like Ist ,second, third from checkboxlist

if i am selecting sunday and make tick on first and last in daytype then I want to get dates(in format dd/MM/yyyy) of first and last sunday within the date range.

how it possible

plz help me

Thanks in advance...

推荐答案

我不确定我是否理解你,但是...



I'm not sure i understand you well, but...

DateTime startdate = new DateTime(2015,1,1);
DateTime enddate = new DateTime(2015,3,1);
	
List<datetime> days = new List<datetime>();

DayOfWeek choosenDay = DayOfWeek.Sunday;
int[] choosenOrder = new int[]{1,3,5};

while(startdate<enddate)>
{
	if(startdate.DayOfWeek==choosenDay)
	{
		days.Add(startdate);
		startdate=startdate.AddDays(6);
	}
        startdate=startdate.AddDays(1);
}
	
//get 1, 3 and 5 sunday
var qry = days.Select((d,i)=>new {day = d, index = (int)i+1}).Join(choosenOrder, item=>item.index, n=>n, (item, n)=>new{day = item.day, index=n});





现在,您可以使用 foreach 列出所有工作日循环:



Now, you can list all weekdays by using foreach loop:

foreach(var d in qry)
{
    Console.WriteLine("{0} - {1}", d.index.ToString(), d.day.ToString("dd-MM-yyyy"));
}


   private static DateTime GetDate(
           DayOfWeek targetdayOfWeek,
           DateTime startDate,
           DateTime endDate,
           int numberOfOccurrences)
       {
           if (startDate > endDate)
           {
               throw new InvalidOperationException("The start date must not be greater than the end date");
           }
           int offset;
           if (targetdayOfWeek < startDate.DayOfWeek)
           {
               offset = 7 - (int)startDate.DayOfWeek + (int)targetdayOfWeek;
           }
           else
           {
               offset = targetdayOfWeek - startDate.DayOfWeek;
           }

  DateTime date = startDate.AddDays(offset + ((numberOfOccurrences - 1) * 7));
           if (date < startDate || date > endDate)
           {
               throw new InvalidOperationException("The number parameter is either too high or too low");
           }

return date;
       }


这篇关于如何在给定的日期范围内从给定的工作日获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆