Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.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
Ios 如何压缩两个日期?_Ios_Iphone_Nsdate_Nsdateformatter_Nscalendar - Fatal编程技术网

Ios 如何压缩两个日期?

Ios 如何压缩两个日期?,ios,iphone,nsdate,nsdateformatter,nscalendar,Ios,Iphone,Nsdate,Nsdateformatter,Nscalendar,我正在iOS项目中使用dslcallenderview。我需要在日历视图的日期中填充颜色。我将特定日期存储到NSMutablearray中,并将其与日期对象进行比较。dslclenderview代码为: - (void)drawBackground { NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setTimeZone:[NSTimeZone systemTimeZon

我正在iOS项目中使用
dslcallenderview
。我需要在
日历视图
的日期中填充颜色。我将特定日期存储到
NSMutablearray
中,并将其与日期对象进行比较。
dslclenderview
代码为:

- (void)drawBackground {
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setTimeZone:[NSTimeZone systemTimeZone]];
    //[dateformatter setLocale:[NSLocale currentLocale]];
    [dateformatter setDateFormat:@"MMMM yyyy dd HH:mm:ss Z"];
    if (self.selectionState == DSLCalendarDayViewNotSelected) {
        user=[[NSUserDefaults alloc] init];

        NSUInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *components = [calendar components:flags fromDate:[self.day date]];

        NSDateComponents *componentsOfToday = [calendar components:flags fromDate:[NSDate date]];

        date = [calendar dateFromComponents:components];

        dateToday = [calendar dateFromComponents:componentsOfToday];

        if (self.isInCurrentMonth) {

            /*
            for (int i=0;i<datearray.count;i++) {

                dateSelected=[dateformatter dateFromString:[datearray objectAtIndex:i]];
            }


            if ([date isEqualToDate:dateSelected]) {

                NSLog(@"date selected......... %@",dateSelected);



                }
            */

        //    NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            if ([date isEqualToDate:dateToday]) {
             //   NSLog(@"inside today......... %@",dateToday);
                [[UIColor redColor] setFill];
                //from now current day is colored
            }
            else {

                //NSLog(@"date selected %@",dateSelected);
                [[UIColor colorWithWhite:245.0 / 255.0 alpha:1.0] setFill];
            }

        }
        else {

           // NSLog(@"++++++++++++++++++++++++++++++");
            if ([date isEqualToDate:dateToday]) {

                [[UIColor greenColor] setFill];

            }
            else {

                [[UIColor colorWithWhite:225.0 / 255.0 alpha:1.0] setFill];
            }

            for (int i=0;i<datearray.count;i++) {

                dateSelected=[dateformatter dateFromString:[datearray objectAtIndex:i]];
                NSLog(@"date %@  dateselected %@",date,dateSelected);
                NSDateComponents *selected=[calendar components:flags fromDate:dateSelected];
                dateSelected=[calendar dateFromComponents:selected];
                NSLog(@"date %@ dateselected %@",date,dateSelected);
                if ([date isEqualToDate:dateSelected]) {
                    NSLog(@"inside loop");
                    [[UIColor grayColor]setFill];
                }
            }


        }
        UIRectFill(self.bounds);
    }
    else {
        switch (self.selectionState) {

            case DSLCalendarDayViewNotSelected:

                NSLog(@"DSLCalendarDayViewNotSelected>>>>+++");

                break;

            case DSLCalendarDayViewStartOfSelection:

                NSLog(@"DSLCalendarDayViewStartOfSelection>>>>+++");
                [[[UIImage imageNamed:@"DSLCalendarDaySelection-left"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];

                break;

            case DSLCalendarDayViewEndOfSelection:
                NSLog(@"DSLCalendarDayViewEndOfSelection>>>>+++");

                [[[UIImage imageNamed:@"DSLCalendarDaySelection-right"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];
                break;

            case DSLCalendarDayViewWithinSelection:

                NSLog(@"DSLCalendarDayViewWithinSelection>>>>+++");



                [[[UIImage imageNamed:@"DSLCalendarDaySelection-middle"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];
                break;

            case DSLCalendarDayViewWholeSelection:

                NSLog(@" DSLCalendarDayViewWholeSelection");


                 NSLog(@"day select %@",selectedday);
                [[NSUserDefaults standardUserDefaults] setObject:selectedday forKey:@"day"];
                [[NSUserDefaults standardUserDefaults]synchronize];


                [[[UIImage imageNamed:@"DSLCalendarDaySelection"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];

                break;
        }
    }
}
-(无效)牵引杆接地{
NSDateFormatter*dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setTimeZone:[NSTimeZone systemTimeZone]];
//[dateformatter setLocale:[NSLocale currentLocale]];
[dateformatter setDateFormat:@“mm yyyy dd HH:mm:ss Z”];
如果(self.selectionState==DSLCalendarDayViewNotSelected){
用户=[[NSUserDefaults alloc]init];
NSInteger flags=NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*components=[calendar components:flags fromDate:[self.day date]];
NSDateComponents*componentsOfToday=[日历组件:flags fromDate:[NSDate日期]];
日期=[calendar dateFromComponents:components];
dateToday=[CalendarDateFromComponents:componentsOfToday];
if(self.isincurrentmount){
/*

for(int i=0;i下面是用于比较我的一个应用程序中使用的两个日期的示例代码。因为我从web服务获取一个字符串日期,第二个是当前日期

NSString *dateStr;//get the date in string here
dateStr=[dateStr substringToIndex: MIN(10, [dateStr length])];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *myEventDate =[[NSDate alloc]init];
NSDate *currentDate =[[NSDate alloc]init];

//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

myEventDate=[dateFormatter dateFromString:dateStr];

//NSLog(@"my Event Date is: %@",myEventDate);
//NSLog(@"current Date %@",currentDate);

NSComparisonResult result = [currentDate compare:myEventDate];

switch (result)
{
    case NSOrderedAscending:

       // NSLog(@"Future Date");
        break;

    case NSOrderedDescending:

        //NSLog(@"Earlier Date");
        break;
    case NSOrderedSame:
        //NSLog(@"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
        break;
    default:
       // NSLog(@"Error Comparing Dates");
        break;
}

我想我找到了答案

  for (int i=0;i<datearray.count;i++) {

            dateSelected=[dateformatter dateFromString:[datearray objectAtIndex:i]];
            NSLog(@"date %@  dateselected %@",date,dateSelected);
            NSDateComponents *selected=[calendar components:flags fromDate:dateSelected];
            dateSelected=[calendar dateFromComponents:selected];
            NSLog(@"date %@ dateselected %@",date,dateSelected);
            if ([dateSelected isEqualToDate:date]) {  //just swapped values and date will be highlighted 
                NSLog(@"inside loop");
                [[UIColor grayColor]setFill];
            }
        }
for(int i=0;我看一下: