2011-06-07 190 views
2
發生

查找第三個星期日的每個月出現以下兩個日期給出之間。找到第三個星期日每個月給兩個日期之間

開始日期: - 07-06-2011 // DD-MM-YYYY

結束日期: - 07-06-2012 // DD-MM-YYYY

USEC#.NET

+7

那你有沒有嘗試什麼? – skajfes 2011-06-07 10:38:46

+3

我們不介意在這裏幫助人們完成作業,Pritesh,但我們不會在沒有你付出一些努力的情況下給你答案。 – DOK 2011-06-07 10:40:56

回答

3

這應該做的伎倆:

public List<DateTime> ThirdSundayOfEachMonth(DateTime startdate, DateTime enddate) 
{ 
    List<DateTime> result = new List<DateTime>(); 
    int sundaymonthcount = 0; 
    for(DateTime traverser = new DateTime(startdate.Year, startdate.Month, 1); traverser <= enddate; traverser = traverser.AddDays(1)){ 
    if(traverser.DayOfWeek == DayOfWeek.Sunday) sundaymonthcount++; 
    if(sundaymonthcount == 3 && traverser > startdate){ 
     result.Add(traverser); 
     sundaymonthcount = 0; 
     traverser = new DateTime(traverser.Year, traverser.Month, 1).AddMonths(1); 
    } 
    } 
return result; 
} 
+0

啊......沒有看到那裏的作業標籤。那麼應該讓他先徘徊多一點。 – 2011-06-07 10:48:45

+0

沒有太多使用循環這麼簡單的任務?這可以在幾個數學運算中輕鬆完成 – 2011-06-07 10:50:13

+0

@Piotr - 大部分是什麼? IT可以通過一個循環來輕鬆完成;) – 2011-06-07 10:53:25

1

要找到第三個星期日月:

var date = new DateTime(year, month, 1);

int daysToAdd = (7 - (int)date.DayOfweek) % 7 + 14;

var sunday = date.AddDays(daysToAdd);

我還沒有測試它,但它應該表現出總體思路

+0

'每個月的第三個星期天? – V4Vendetta 2011-06-07 11:08:30

+0

爲固定的月份,如果你想要每個月都應該在循環中完成12個月 – 2011-06-07 11:12:24

+0

問題不在於找到某個月的第三個星期日,而是在一個日期範圍內。所以我想你的方法也需要在某處放置一個循環。但是找到第三個星期天的方式應該沒問題,只是假設星期日是一週的第七天,而在DateTime中是第一個。 – 2011-06-07 11:13:27

0
> using System; using System.Collections.Generic; using System.Linq; 
> using System.Web; using System.Web.UI; using 
> System.Web.UI.WebControls; 
> 
> namespace vbvb { 
>  public partial class _Default : Page 
>  { 
>   protected void Page_Load(object sender, EventArgs e) 
>   { 
>    if (!IsPostBack) 
>    { 
>     DateTime today = DateTime.Today; 
>     DateTime otherday = DateTime.Today.AddYears(1); 
> 
>     Calendar1.TodaysDate = today; 
>     Calendar1.SelectedDate = Calendar1.TodaysDate; 
>     Calendar2.TodaysDate = otherday; 
>     Calendar2.SelectedDate = Calendar2.TodaysDate; 
> 
>     TextBox1.Text = Calendar1.SelectedDate.ToString("dd-MMM-yy"); 
>     TextBox2.Text = Calendar2.SelectedDate.ToString("dd-MMM-yy"); 
> 
>     var items1 = new List<ListItem>(); 
>     items1.Add(new ListItem("", "")); 
>     items1.Add(new ListItem { Text = "Month", Value = "month" }); 
>     items1.Add(new ListItem { Text = "Month + 2", Value = "2month" }); 
>     items1.Add(new ListItem { Text = "Month + 5", Value = "5month" }); 
>     items1.Add(new ListItem { Text = "Year", Value = "year" }); 
>     drpMonths.DataSource = items1; 
>     drpMonths.DataTextField = "Text"; 
>     drpMonths.DataValueField = "Value"; 
>     drpMonths.DataBind(); 
> 
>     var ddWeekdays = new List<ListItem>(); 
>     ddWeekdays.Add(new ListItem("", "")); 
>     ddWeekdays.Add(new ListItem { Text = "Sunday", Value = "sunday" }); 
>     ddWeekdays.Add(new ListItem { Text = "Monday", Value = "Monday" }); 
>     ddWeekdays.Add(new ListItem { Text = "Tuesday", Value = "Tuesday" }); 
>     ddWeekdays.Add(new ListItem { Text = "Wednesday", Value = "Wednesday" }); 
>     ddWeekdays.Add(new ListItem { Text = "Thursday", Value = "Thursday" }); 
>     ddWeekdays.Add(new ListItem { Text = "Friday", Value = "Friday" }); 
>     ddWeekdays.Add(new ListItem { Text = "Saturday", Value = "Saturday" }); 
>     drpWeekdays.DataSource = ddWeekdays; 
>     drpWeekdays.DataTextField = "Text"; 
>     drpWeekdays.DataValueField = "Value"; 
>     drpWeekdays.DataBind(); 
> 
>     var ddinterval = new List<ListItem>(); 
>     ddinterval.Add(new ListItem("", "")); 
>     ddinterval.Add(new ListItem { Text = "1", Value = "1" }); 
>     ddinterval.Add(new ListItem { Text = "2", Value = "2" }); 
>     ddinterval.Add(new ListItem { Text = "3", Value = "3" }); 
>     ddinterval.Add(new ListItem { Text = "4", Value = "4" }); 
>     ddinterval.Add(new ListItem { Text = "5", Value = "5" }); 
>     drpInterval.DataSource = ddinterval; 
>     drpInterval.DataTextField = "Text"; 
>     drpInterval.DataValueField = "Value"; 
>     drpInterval.DataBind(); 
>    } 
>   } 
> 
>   protected void TextBox2_TextChanged(object sender, EventArgs e) 
>   { 
> 
>   } 
> 
>   protected void RadioButton2_CheckedChanged(object sender, EventArgs e) 
>   { 
> 
>   } 
> 
>   protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
>   { 
>    TextBox1.Text = Calendar1.SelectedDate.ToString("dd-MMM-yy"); 
>   } 
> 
>   protected void Calendar2_SelectionChanged(object sender, EventArgs e) 
>   { 
>    TextBox2.Text = Calendar2.SelectedDate.ToString("dd-MMM-yy"); 
>   } 
> 
>   protected void btnSubmit_Click(object sender, EventArgs e) 
>   { 
>    var fromDate = Calendar1.SelectedDate; 
>    var todate = Calendar2.SelectedDate; 
>    var dateList = new List<DateTime>(); 
>    if (fromDate != null && todate != null && (todate > fromDate)) 
>    { 
>     var fDate = fromDate; 
>     bool flag = true; 
>     while (fDate < todate) 
>     { 
>      if (!string.IsNullOrEmpty(drpMonths.SelectedValue)) 
>      { 
>       flag = CheckMonth(fDate); 
>      } 
>      if (flag && !string.IsNullOrEmpty(drpWeekdays.SelectedValue)) 
>      { 
>       flag = CheckDay(fDate); 
>      } 
>      if (flag && !string.IsNullOrEmpty(drpInterval.SelectedValue)) 
>      { 
>       flag = CheckInterval(fDate); 
>      } 
>      if (flag) 
>      { 
>       dateList.Add(fDate); 
>      } 
>      fDate = fDate.AddDays(1); 
>      flag = true; 
>     } 
>     BulletedList1.DataSource = dateList; 
>     BulletedList1.DataBind(); 
>    } 
>   } 
> 
>   public DateTime? LastExecutedMonth = null; 
>   private bool CheckMonth(DateTime date) 
>   { 
>    var retVal = false; 
>    if (LastExecutedMonth == null) 
>    { 
>     LastExecutedMonth = Calendar1.SelectedDate; 
>    } 
>    
>    switch (drpMonths.SelectedValue.ToLower()) 
>    { 
>     case "month": 
>      if (MonthDiff(Convert.ToDateTime(LastExecutedMonth), date) == 1) 
>      { 
>       retVal = true; 
>       if (date == new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1)) 
>       { 
>        LastExecutedMonth = date; 
>       } 
>      } 
>      break; 
>     case "2month": 
>      if (MonthDiff(Convert.ToDateTime(LastExecutedMonth), date) == 2) 
>      { 
>       retVal = true; 
>       if (date == new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1)) 
>       { 
>        LastExecutedMonth = date; 
>       } 
>      } 
>      break; 
>     case "5month": 
>      if (MonthDiff(Convert.ToDateTime(LastExecutedMonth), date) == 5) 
>      { 
>       retVal = true; 
>       if (date == new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1)) 
>       { 
>        LastExecutedMonth = date; 
>       } 
>      } 
>      break; 
>     case "year": 
>      if (MonthDiff(Convert.ToDateTime(LastExecutedMonth), date) == 12) 
>      { 
>       retVal = true; 
>       if (date == new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1)) 
>       { 
>        LastExecutedMonth = date; 
>       } 
>      } 
>      break; 
>    } 
>    return retVal; 
>   } 
> 
>   private bool CheckDay(DateTime date) 
>   { 
>    var retval = false; 
>    switch (drpWeekdays.SelectedValue.ToLower()) 
>    { 
>     case "sunday": 
>      retval = date.DayOfWeek == DayOfWeek.Sunday; 
>      break; 
>     case "monday": 
>      retval = date.DayOfWeek == DayOfWeek.Monday; 
>      break; 
>     case "tuesday": 
>      retval = date.DayOfWeek == DayOfWeek.Tuesday; 
>      break; 
>     case "wednesday": 
>      retval = date.DayOfWeek == DayOfWeek.Wednesday; 
>      break; 
>     case "thursday": 
>      retval = date.DayOfWeek == DayOfWeek.Thursday; 
>      break; 
>     case "friday": 
>      retval = date.DayOfWeek == DayOfWeek.Friday; 
>      break; 
>     case "saturday": 
>      retval = date.DayOfWeek == DayOfWeek.Saturday; 
>      break; 
>    } 
>    return retval; 
>   } 
> 
>   private bool CheckInterval(DateTime date) 
>   { 
>    var flag = false; 
>    switch (drpInterval.SelectedValue.ToLower()) 
>    { 
>     case "1": 
>      flag = date == XthDayOfWeekInMonth(date.Year, date.Month, date.DayOfWeek, 1); 
>      break; 
>     case "2": 
>      flag = date == XthDayOfWeekInMonth(date.Year, date.Month, date.DayOfWeek, 2); 
>      break; 
>     case "3": 
>      flag = date == XthDayOfWeekInMonth(date.Year, date.Month, date.DayOfWeek, 3); 
>      break; 
>     case "4": 
>      flag = date == XthDayOfWeekInMonth(date.Year, date.Month, date.DayOfWeek, 4); 
>      break; 
>     case "5": 
>      flag = date == XthDayOfWeekInMonth(date.Year, date.Month, date.DayOfWeek, 5); 
>      break; 
>    } 
>    return flag; 
>   } 
> 
>   public static DateTime FirstDayOfWeekInMonth(int year, int month, DayOfWeek day) 
>   { 
>    DateTime res = new DateTime(year, month, 1); 
>    int offset = -(res.DayOfWeek - day); 
> 
>    if (offset < 0) 
>     offset += 7; 
> 
>    res = res.AddDays(offset); 
> 
>    return res; 
>   } 
> 
>   public static DateTime LastDayOfWeekInMonth(int year, int month, DayOfWeek day) 
>   { 
>    DateTime res = FirstDayOfWeekInMonth(year, month + 1, day); 
> 
>    res = res.AddDays(-7); 
> 
>    return res; 
>   } 
> 
>   public static DateTime XthDayOfWeekInMonth(int year, int month, DayOfWeek day, int x) 
>   { 
>    DateTime res = DateTime.MinValue; 
> 
>    if (x > 0) 
>    { 
>     res = FirstDayOfWeekInMonth(year, month, day); 
> 
>     if (x > 1) 
>      res = res.AddDays((x - 1) * 7); 
> 
>     res = res.Year == year && res.Month == month ? res : DateTime.MinValue; 
>    } 
> 
>    return res; 
>   } 
> 
>   public static int MonthDiff(DateTime date1, DateTime date2) 
>   { 
>    if (date1.Month < date2.Month) 
>    { 
>     return (date2.Year - date1.Year) * 12 + date2.Month - date1.Month; 
>    } 
>    else 
>    { 
>     return (date2.Year - date1.Year - 1) * 12 + date2.Month - date1.Month + 12; 
>    } 
>   } 
>  } } 
相關問題