我正在尝试解析RadCalendar日期并禁用事件开始日期之前的日期。
我们从数据库中获取StartDateTime,并且我想从Future StartDateTime中恢复日期,直到当前(本月)的开头。
以下是我们目前拥有的代码,但它只回顾i< 31.这就是为什么我想将DateTime作为一个int的天数一直回到当月的开头(第一个)。
if (nextAvailableTime != null && nextAvailableTime.StartDateTime > DateTime.Today)
{
//DISABLE dates prior to next available date
DateTime dt = nextAvailableTime.StartDateTime.AddDays(-1);
for (var i = 0; i < 31; i++) //Would like to change this to beginning of current month.
{
tkCalendar.SpecialDays.Add(new RadCalendarDay(tkCalendar) { Date = dt.Date.AddDays(i * -1), IsDisabled = true, IsSelectable = false });
}
}
答案 0 :(得分:2)
为什么不减去2个日期并获得天数差异?我使用自己的变量,因为我不清楚你的变量是什么。我的循环禁用前进而不是乘以-1。您可能需要将循环编辑为&lt; =或从1开始,具体取决于您是否要包含第一个和最后一个日期。
if (nextAvailableTime != null && nextAvailableTime.StartDateTime > DateTime.Today)
{
//DISABLE dates prior to next available date
DateTime currentDate = DateTime.Now;
DateTime futureDate = DateTime.Now.AddMonths(3);
int daysBetween = (futureDate - currentDate).Days;
for (var i = 0; i < daysBetween; i++)
{
tkCalendar.SpecialDays.Add(new RadCalendarDay(tkCalendar) { Date = currentDate.AddDays(i), IsDisabled = true, IsSelectable = false });
}
}
答案 1 :(得分:0)
我们提出的答案是获取下一个可用日期,然后是当月的开始日期,并使用DayOfYear获取差异。
解决方案如下:
if (nextAvailableTime != null && nextAvailableTime.StartDateTime > DateTime.Today)
{
//DISABLE dates prior to next available date
DateTime dt = nextAvailableTime.StartDateTime.AddDays(-1);
DateTime nextDate = nextAvailableTime.StartDateTime;
//Gate the calendar to just go get the product's next available date and then get block out everything until the beginning of the current month.
var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
TimeSpan daysBetween = (futureDate - startOfMonth);
// for (var i = 0; i < 31; i++)//Original from 31 days from next available.
for (var i = 0; i < daysBetween.Days; i++) //Get difference between next available and beginning of current month.
{
tkCalendar.SpecialDays.Add(new RadCalendarDay(tkCalendar) { Date = dt.Date.AddDays(i * -1), IsDisabled = true, IsSelectable = false });
}
}