C#web窗体 - 寻找获取从日历中选定日期后的月份的第一个星期一的日期

问题描述:

我目前正在Visual C#中创建一个Web窗体,该应用程序将成为一个财务计算器。C#web窗体 - 寻找获取从日历中选定日期后的月份的第一个星期一的日期

以下大部分代码都是无关紧要的,仅用于显示整个页面,详见标题,我希望能够从Web表单上的日历中选择一个日期,然后从中取出日期并将其显示在标签上。我现在可以选择一个日期并显示。

namespace FinanceCalc 
{ 
    public partial class About : Page 
    { 
     decimal NumPmt = 0; 

     protected void Page_Load(object sender, EventArgs e) 
     { 
     } 

     protected void Price_TextChanged(object sender, EventArgs e) 
     { 
      if (String.IsNullOrEmpty(Price.Text)) 
      { 
       Response.Write("Please enter the Vehicle Price"); 
      } 
     } 

     protected void DepositCalc_Click(object sender, EventArgs e) 
     { 
      if (String.IsNullOrEmpty(Price.Text)) 
      { 
       Response.Write("Please enter the Vehicle Price"); 
      } 
      else 
      { 
       decimal price = Convert.ToDecimal(Price.Text); 
       decimal depositamount = (price/100 * 15); 

       Deposit.Text = depositamount.ToString(); 
      } 
     } 

     protected void FinanceCalc_Click(object sender, EventArgs e) 
     { 
      // This works out if all required boxes have been populated and displays a warning message if they have not been. 
      if (String.IsNullOrEmpty(Price.Text)) 
      { 
       Response.Write("Please enter the Vehicle Price."); 
      } 

      if (String.IsNullOrEmpty(Deposit.Text)) 
      { 
       Response.Write("Please calculate the deposit."); 
      } 

      if (String.IsNullOrEmpty(Length.Text)) 
      { 
       Response.Write("Please enter the finance length."); 
      } 

      if (Convert.ToInt32(Length.Text) < 1) 
      { 
       Response.Write("Please choose between 1, 2 and 3 years."); 
      } 

      if (Convert.ToInt32(Length.Text) > 3) 
      { 
       Response.Write("Please choose between 1, 2 and 3 years."); 
      } 
      else 
      { 
       // This works out payment amounts 
       NumPmt = 12 * int.Parse(Length.Text); 
       decimal price = Convert.ToDecimal(Price.Text); 
       decimal deposit = Convert.ToDecimal(Deposit.Text); 
       decimal MonthlyPayments = (price - deposit)/NumPmt; 

       // This populates the Finance Info text box with the information 
       Results.Text = "Deposit Amount - £" + Deposit.Text + Environment.NewLine + "Arrangement Fee - £88" + Environment.NewLine + "Completion Fee - £20" + Environment.NewLine + "Number of Payments: " + NumPmt 
       + Environment.NewLine + "Delivery Date: " + DeliveryDate.SelectedDate + Environment.NewLine + "First Payment Date: " + Environment.NewLine + "First Payment Amount: £" + MonthlyPayments + " plus your £88 Arrangement Fee." 
       + Environment.NewLine + "Monthly Payments: £" + MonthlyPayments + " for " + (12 * int.Parse(Length.Text) - 2) + " months." + Environment.NewLine + "Final Payment Amount: £" + MonthlyPayments + " plus your £20 Completion Fee"; 
      } 
     } 

     protected void Reset_Click(object sender, EventArgs e) 
     { 
      //This resets all of the information boxes, allowing the user to start again 
      Price.Text = ""; 
      Deposit.Text = ""; 
      Results.Text = ""; 
     } 

     protected void Length_TextChanged(object sender, EventArgs e) 
     { 
      // This works out how many monthly payments will be made. 
      NumPmt = 12 * int.Parse(Length.Text); 
     } 
    } 
}  
+0

如果用户选择每月的第一个星期一,会发生什么?你显示那一个还是下一个月的? – Xiaoy312

+0

看看这篇文章,并得到本月的第一天..然后从那里你可以检查星期几是星期一..星期日...等https://*.com/questions/25233520/how-这是一个月的日历 - 查看 - 星期日 - 星期六,这实际上并不像您想象的那么困难..您可以使用许多日期和月份功能。 C#,https://*.com/questions/25233520/how-to-get-the-first-and-last-day-of-a-month-calendar-view-sunday-saturday – MethodMan

这应该做的工作:

var selectedDate = new DateTime(2018, 5, 2); 
var firstMonday = Enumerable.Range(0, 2) 
    .SelectMany(monthOffset => Enumerable.Range(1, 7) 
     .Select(day => new DateTime(date.Year, date.Month + monthOffset, day))) 
    .Where(x => x.DayOfWeek == DayOfWeek.Monday) 
    .First(x => x > selectedDate);