public static IEnumerable<DateTime> MonthsBetween(this DateTime date1, DateTime date2) { for (var date = new[] { date1, date2 }.Min().StartOfMonth(); date <= new[] { date1, date2 }.Max(); date = date.AddMonths(1)) yield return date.StartOfMonth(); }
private void UpdateSumm(System.DateTime dateBegin) { System.DateTime period = dateBegin.AddMonths(1).AddDays((double) -1.0); foreach (System.Windows.Forms.DataGridViewRow row in (System.Collections.IEnumerable) this.dgvAccountService.Rows) { AccountService service = row.get_DataBoundItem() as AccountService; row.Cells.get_Item("PaymentDataGridViewTextBoxColumn").set_Value(0); row.Cells.get_Item("ChargeDataGridViewTextBoxColumn").set_Value(service.GetSummByGroup(dateBegin, "НачислениеФактическое")); row.Cells.get_Item("BalanceDataGridViewTextBoxColumn").set_Value(service.GetSummByGroup(period, "Сальдо")); } this.ValidatedTotalSumm(); }
//Creates a title block. No customization is offered, if that's needed then specify the header template. Control CreateStaticTitle(DateTime visibleDate, System.Globalization.Calendar threadCalendar) { //Create a new table for the header controls Table titleTable = new Table(); titleTable.GridLines = GridLines.None; titleTable.Width = Unit.Percentage(100); titleTable.CellSpacing = 0; TableRow titleTableRow = new TableRow(); titleTable.Rows.Add(titleTableRow); TableCell PrevCell = new TableCell(); titleTableRow.Cells.Add(PrevCell); PrevCell.ApplyStyle(nextPrevStyle); Button PrevBtn = new Button(); PrevBtn.CssClass = "buttonlink"; PrevBtn.Text = "< " + threadCalendar.AddMonths(visibleDate, -1).ToString("MMMM"); PrevBtn.CommandName = COMMAND_PREVMONTH; PrevCell.Controls.Add(PrevBtn); TableCell MonthCell = new TableCell(); titleTableRow.Cells.Add(MonthCell); MonthCell.ApplyStyle(titleStyle); MonthCell.Text = visibleDate.ToString("MMMM yyyy"); TableCell NextCell = new TableCell(); titleTableRow.Cells.Add(NextCell); NextCell.ApplyStyle(nextPrevStyle); Button NextBtn = new Button(); NextBtn.CssClass = "buttonlink"; NextBtn.Text = threadCalendar.AddMonths(visibleDate, +1).ToString("MMMM") + " >"; NextBtn.CommandName = COMMAND_NEXTMONTH; NextCell.Controls.Add(NextBtn); return titleTable; }
/// <summary>Months Between 2 dates this will output a decimal to 0dp /// <para>StartDate = Start Date for the calculation</para> /// <para>EndDate = End Date for the calculation</para> /// <para>roundup = round up months from mid point up</para> /// </summary> public int GetMonthsBetween(System.DateTime StartDate, System.DateTime EndDate, bool roundup) { if(roundup == true) { EndDate = EndDate.AddDays(1); } var monthDiff = Math.Abs((EndDate.Year * 12 + (EndDate.Month - 1)) - (StartDate.Year * 12 + (StartDate.Month - 1))); if (StartDate.AddMonths(monthDiff) > EndDate || EndDate.Day < StartDate.Day) { return monthDiff - 1; } else { return monthDiff; } }