Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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
Iphone 向日期数组添加时间时出现的问题_Iphone_Ios_Nsdate_Nsdateformatter - Fatal编程技术网

Iphone 向日期数组添加时间时出现的问题

Iphone 向日期数组添加时间时出现的问题,iphone,ios,nsdate,nsdateformatter,Iphone,Ios,Nsdate,Nsdateformatter,我正在尝试将小时和分钟添加到日期数组中,下面是我的代码,我已将旧数组的NSLog和数组后的NSLog放在一起 NSDate* date= _timePicker.date; NSDateFormatter *formatter=[[[NSDateFormatter alloc]init]autorelease]; [formatter setDateFormat:@"hh:mm"]; [formatter setTimeZone:[NSTimeZone system

我正在尝试将小时和分钟添加到日期数组中,下面是我的代码,我已将旧数组的NSLog和数组后的NSLog放在一起

 NSDate* date= _timePicker.date;
    NSDateFormatter *formatter=[[[NSDateFormatter alloc]init]autorelease];
    [formatter setDateFormat:@"hh:mm"];


    [formatter setTimeZone:[NSTimeZone systemTimeZone]];

    [formatter setTimeStyle:NSDateFormatterLongStyle];

    NSDateFormatter *formatter1=[[[NSDateFormatter alloc]init]autorelease];
    [formatter1 setDateFormat:@"dd:MM:yyyy"];


    [formatter1 setTimeZone:[NSTimeZone systemTimeZone]];

    [formatter1 setTimeStyle:NSDateFormatterLongStyle];


    NSDateFormatter *formatter3=[[[NSDateFormatter alloc]init]autorelease];
    [formatter3 setDateFormat:@"dd-MM-yyyy hh:mm"];

    [formatter3 setTimeZone:[NSTimeZone systemTimeZone]];

    [formatter3 setTimeStyle:NSDateFormatterLongStyle];

    NSString *timeselected =[formatter stringFromDate:date];

    NSLog(@"we have picked time %@",timeselected);


    NSDate *date2 = [formatter dateFromString:timeselected];

    // NSLog(@"date2%@",dateSelected );

       NSLog(@"old%@",delegate.notificationBirthDateArray);
old(
    "15/08/2013",
    "15/07/2014",
    "15/07/2014",
    "07/06/2014",
    "15/01/2014",
    "27/01/2014",
    "20/01/2014",
    "22/06/2014",
    "29/08/2013",
    "15/06/2014",
    "16/07/2013"
)


    for (int i=0; i<[delegate.notificationBirthDateArray count]; i++) {



        NSDate *date1 = [formatter1 dateFromString:[delegate.notificationBirthDateArray objectAtIndex:i]];
     //   NSLog(@"date2%@",date2);
     //   NSLog(@"array%@",date1);

        NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        // Extract date components into components1
        NSDateComponents *components1 = [gregorianCalendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                                             fromDate:date1];

        // Extract time components into components2
        NSDateComponents *components2 = [gregorianCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:date2];

        // Combine date and time into components3
        NSDateComponents *components3 = [[NSDateComponents alloc] init];

        [components3 setYear:components1.year];
        [components3 setMonth:components1.month];
        [components3 setDay:components1.day];

        [components3 setHour:components2.hour];
        [components3 setMinute:components2.minute];
        // when i nslog this components they are printed perfectly

        // Generate a new NSDate from components3.
        NSDate *combinedDate = [gregorianCalendar dateFromComponents:components3];   

        // combinedDate contains both your date and time!

          NSString *lastdate =[formatter3 stringFromDate:combinedDate];

       NSLog(@"combinedDate%@",lastdate);



        [delegate.notificationBirthDateArray removeObjectAtIndex:i];
        [delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];



    }
 NSLog(@"new%@",delegate.notificationBirthDateArray);
new(
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30",
    "6:00:00 PM GMT+05:30"
)
NSDate*date=\u timePicker.date;
NSDateFormatter*格式化程序=[[[NSDateFormatter alloc]init]autorelease];
[格式化程序setDateFormat:@“hh:mm”];
[格式化程序设置时区:[NSTimeZone systemTimeZone]];
[格式化程序setTimeStyle:NSDateFormatterLongStyle];
NSDateFormatter*格式化程序1=[[[NSDateFormatter alloc]init]autorelease];
[formatter1 setDateFormat:@“dd:MM:yyyy”];
[formatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[formatter1 setTimeStyle:NSDateFormatterLongStyle];
NSDateFormatter*formatter3=[[NSDateFormatter alloc]init]autorelease];
[formatter3 setDateFormat:@“dd-MM-yyy-hh:MM”];
[formatter3 setTimeZone:[NSTimeZone systemTimeZone]];
[formatter3 setTimeStyle:NSDateFormatterLongStyle];
NSString*timeselected=[formatter stringFromDate:date];
NSLog(@“我们已选择时间%@”,TimesSelected);
NSDate*date2=[formatter dateFromString:timeselected];
//NSLog(@“date2%@”,已选择日期);
NSLog(@“旧%@”,delegate.notificationBirthDateArray);
旧的(
"15/08/2013",
"15/07/2014",
"15/07/2014",
"07/06/2014",
"15/01/2014",
"27/01/2014",
"20/01/2014",
"22/06/2014",
"29/08/2013",
"15/06/2014",
"16/07/2013"
)
对于(int i=0;i我在这里看到一个问题:

for (int i=0; i<[delegate.notificationBirthDateArray count]; i++) {
     //Your code
     [delegate.notificationBirthDateArray removeObjectAtIndex:i];                    
     [delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];
}
然后,如果要执行此操作:

for (int i=0; i<arr.count; i++) 
    {
         //Your code
         [delegate.notificationBirthDateArray removeObjectAtIndex:i];                    
         [delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];
    }
并修改行:

 [formatter1 setDateFormat:@"dd:MM:yyyy"];
致:


如果字符串日期与旧数组中的日期完全相同;

要向NSDate添加小时/分钟时间间隔,请使用
dateWithTimeInterval:sinceDate:

NSDate* oldDate = <some date>;
NSTimeInterval timeToAdd = ((hoursToAdd * 60) + minutesToAdd) * 60.0;
NSDate* newDate = [theDate dateWithTimeInterval:timeToAdd sinceDate:oldDate];

(或者,如果您不需要NSDate,请完全跳过日期格式化程序。)

hi@soryngod如果数组是
NSMutableArray
可能会出现什么问题?请告诉我?@preetam例如,您可能会遇到一个异常,数组在枚举时发生了变异。@soryngod与以前相同的输出只有time no dates ew(“格林尼治标准时间下午6:28:00+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:30”、“格林尼治标准时间下午6:28+05:00”我不知道nslog date1显示为null为什么会发生这种情况是因为我的格式化程序错误?我想是的,您的格式化可能错误,我现在检查一下
[formatter1 setTimeStyle:NSDateFormatterLongStyle];
 [formatter1 setDateFormat:@"dd:MM:yyyy"];
[formatter1 setDateFormat:@"dd/MM/yyyy"];
NSDate* oldDate = <some date>;
NSTimeInterval timeToAdd = ((hoursToAdd * 60) + minutesToAdd) * 60.0;
NSDate* newDate = [theDate dateWithTimeInterval:timeToAdd sinceDate:oldDate];
NSString* oldDate = @"22/06/14";
NSString* dateWithTime = [oldDate appendString:@" 6:00:00 PM GMT+05:30"];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = @"dd/MM/yy h:mm:ss A 'GMT'ZZZZZ";  (or something like that)
NSDate* scannedDate = [df dateFromString:dateWithTime];