繁体   English   中英

将时间添加到日期数组中的问题

[英]Issue in adding time to array of date

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

}

我在这里看到一个问题:

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

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

要将小时/分钟的时间间隔添加到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,则完全跳过日期格式化程序。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM