Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#日期时间到整数和日历_C#_Datetime - Fatal编程技术网

C#日期时间到整数和日历

C#日期时间到整数和日历,c#,datetime,C#,Datetime,我试图解析出一个日历日期,并禁用事件开始日期之前的日期 我们从数据库中获取StartDateTime,我想禁用从未来StartDateTime到当前(本)月初的日期 编辑:更具体 示例:我的StartDateTime在2014年11月,但我想禁用从该未来日期到本月初(本月为2014年8月)的所有日期。 下面是我们目前拥有的代码,但它只是回顾一下iDateTime.Today) { //禁用下一个可用日期之前的日期 DateTime dt=nextAvailableTime.StartDateTi

我试图解析出一个日历日期,并禁用事件开始日期之前的日期

我们从数据库中获取StartDateTime,我想禁用从未来StartDateTime到当前(本)月初的日期

编辑:更具体 示例:我的StartDateTime在2014年11月,但我想禁用从该未来日期到本月初(本月为2014年8月)的所有日期。 下面是我们目前拥有的代码,但它只是回顾一下i<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 });
            }
        }
if(nextAvailableTime!=null&&nextAvailableTime.StartDateTime>DateTime.Today)
{
//禁用下一个可用日期之前的日期
DateTime dt=nextAvailableTime.StartDateTime.AddDays(-1);
for(var i=0;i<31;i++)//希望将其更改为本月初。
{
tkCalendar.SpecialDays.Add(新RadCalendarDay(tkCalendar){Date=dt.Date.AddDays(i*-1),IsDisabled=true,IsSelectable=false});
}
}

为什么不减去这两个日期,以天为单位计算差值?我使用了我自己的变量,因为我不清楚你的变量是什么。我的循环是禁用前进,而不是乘以-1。您可能需要将循环编辑为DateTime.Today) { //禁用下一个可用日期之前的日期 DateTime currentDate=DateTime.Now; DateTime futureDate=DateTime.Now.AddMonths(3); int daysBetween=(未来日期-当前日期).Days; 对于(变量i=0;i我们得出的答案是先得到下一个可用日期,然后是当前月份的开始日期,然后使用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 });
                }
        }
if(nextAvailableTime!=null&&nextAvailableTime.StartDateTime>DateTime.Today)
{
//禁用下一个可用日期之前的日期
DateTime dt=nextAvailableTime.StartDateTime.AddDays(-1);
DateTime nextDate=nextAvailableTime.StartDateTime;
//关闭日历,只需获取产品的下一个可用日期,然后在本月初之前屏蔽所有内容。
var now=DateTime.now;
var startOfMonth=新日期时间(now.Year,now.Month,1);
TimeSpan Days Between=(futureDate-startOfMonth);
//for(var i=0;i<31;i++)//从下一个可用日期起31天内的原始版本。
for(var i=0;i
这不是我想要的。我更新了我的问题,使之更加具体。它实际上也在做同样的事情。我修改了我的答案,如果我误解了,请告诉我/这或多或少是我需要的。但这件事是,我们从来都不知道未来的日期,可能是3个月,可能是6个月,可能是1个月。更动态的解决方案更好。我确实看到了上面Pete的建议,这或多或少是你正在做的,但是通过设置(3)以一种更静态的方式。但这对我来说是正确的。我只为一个测试变量添加了三个月,所以你可以看到这个概念。我不知道您是如何获得未来日期的,但在我的代码中,任何日期都可以替代
futureDate
。为什么不使用DateTime.DayOfYear并从futuredDayOfYear中减去当前DayOfYear以获得我应该增加到的值?如果未来日期在下一个日历年中,此代码将不起作用(
daysBetween
将为负数)。您只需减去日期,就可以像我在回答中显示的那样以天为单位得到结果。您完全正确。我使用天和时间跨度将代码更新为更好的解决方案。感谢您捕捉到这一点。为我节省了很多未来的挫折。我看到您更新的代码,看起来您采用了与我的回答相同的方法。很高兴提供帮助。
  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 });
                }
        }