c# DateTime转换为Int和RadCalendar

本文关键字:RadCalendar Int DateTime 转换 | 更新日期: 2023-09-27 18:16:36

我正在尝试解析RadCalendar Date并禁用事件开始日期之前的日期。

我们从数据库中获取StartDateTime,我想禁用Future StartDateTime中的日期,直到当前(这个)月初。

编辑:更具体的

示例:我的StartDateTime是在2014年11月,但我想禁用所有日期从未来的日期,直到回到这个月的开始(这个月是2014年8月)。

下面是我们目前拥有的代码,但它只是回顾一下。31. 这就是为什么我想把DateTime的天数作为一个整型,一直返回到当前月的开始(1号)。

 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 });
            }
        }

c# DateTime转换为Int和RadCalendar

为什么不把这两个日期相减,得到天数之差呢?我用了我自己的变量,因为我不清楚你的变量是什么。我的循环是禁止向前而不是乘以-1。您可能需要将循环编辑为<=或从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 });
        }
   }

我们想到的答案是先获得下一个可用的日期,然后是当前月份的开始日期,然后使用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 });
                }
        }