如何获得只有几小时和几分钟

时间:2015-12-11 06:44:38

标签: ios

我试着从日期开始提取小时,但是我得到的是小时,分钟和秒。如何拆分字符串。

NSArray *arr = [scheduling.scheduleFrom componentsSeparatedByString:@"T"];
NSString *schFromTime = [arr objectAtIndex:1];
NSArray *arrFrom = [schFromTime componentsSeparatedByString:@":00"];
NSString *timeFrom=[arrFrom  objectAtIndex:0];

NSArray *arr2 = [scheduling.scheduleTo componentsSeparatedByString:@"T"];
NSString *schtoTime = [arr2 objectAtIndex:1];
NSArray *arrTo = [schtoTime componentsSeparatedByString:@":"];
NSString *timeTo=[arrTo objectAtIndex:0];
ltimeslot.text=[NSString stringWithFormat:@"%@    %@",timeFrom,timeTo];

2 个答案:

答案 0 :(得分:3)

你真的想要什么?小时和分钟两个日期之间的差异?

看到了这个
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1];
NSString diffString = [self stringFromTimeInterval:diff]; 

stringFromTimeInterval函数定义 -

-(NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
    NSInteger ti = (NSInteger)interval;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600);
    return [NSString stringWithFormat:@"%02ld:%02ld", (long)hours, (long)minutes];
 }

答案 1 :(得分:0)

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components =
                    [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];