0

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

}
4

2 回答 2

1

我在这里看到一个问题:

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

你不应该notificationBirthDateArray在 for 循环中使用并对其执行操作,它会带来意想不到的问题,所以我建议在 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"];

如果您的字符串日期与旧数组中的完全一样;

于 2013-07-16T12:44:17.813 回答
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,请完全跳过日期格式化程序。)

于 2013-07-16T15:21:34.817 回答