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

时间:2013-07-16 12:37:02

标签: iphone ios nsdate nsdateformatter

我正在尝试将小时和分钟添加到日期数组以下是我的代码,我已经把旧数组的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"
)

问题是获得的时间会以正确的方式添加,您可以在上面看到,但我的日期在哪里?他们也应该在那里。

请帮助我。

edited and imporved code but still same problem

    NSArray*arr = [NSArray arrayWithArray:delegate.notificationBirthDateArray];


    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"];



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

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


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

       NSString *datefromarray = [[NSString alloc]initWithString:[delegate.notificationBirthDateArray objectAtIndex:i]];


        NSDate *date1 = [formatter1 dateFromString:datefromarray];
   // NSLog(@"hour time %@",[delegate.notificationBirthDateArray objectAtIndex:i]);
       NSLog(@"hour time %@",date2);
        NSLog(@"date%@",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];


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

}

2 个答案:

答案 0 :(得分:1)

我在这里看到一个问题:

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

你不应该在for循环中使用notificationBirthDateArray并对其执行操作,它会带来意想不到的问题,所以我建议在for循环中使用另一个数组作为占位符。

NSArray*arr = [NSArray arrayWithArray:delegate.notificationBirthDateArray];

然后如果你想这样做:

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

可能会有更多的事情,但我认为你应该像我提到的那样修改并发布新的结果。

从formatter1中删除以下行:

[formatter1 setTimeStyle:NSDateFormatterLongStyle];

并修改该行:

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

到:

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

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

答案 1 :(得分:0)

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

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

您也可以使用NSDateComponents,或者格式化日期,操纵字符,然后将其扫描回NSDate,但这两行都需要几十行并且充满了错误的机会。

(但是,如果您从字符日期开始,并且您只想添加“锅炉板”时间值,只需将各个部分连接在一起并扫描结果:

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];

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