0

我有一个提取的 NSString 00:50,如果它多于或少于 30 分钟,我需要对其进行调节。

4

3 回答 3

3

如果字符串具有固定格式 HH:MM,那么您可以只比较字符串:

NSString *timeStr = ...; // time in HH:MM format
if ([timeStr compare:@"00:30"] == NSOrderedAscending) {
    // less than 30 minutes
} else {
    // 30 minutes or more
}
于 2013-08-14T18:36:59.117 回答
2

您正在寻找的是NSDateFormatter.

NSString *time = @"00:50";
NSDateFormatter *timeConverter = [NSDateFormatter new];
[timeConverter setDateFormat:@"HH:mm"];

NSDate *date = [timeConverter dateFromString:time];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds = [components second];

if (minutes + (hours * 60) > 30) {

    // Do your thing here.
}

从那里,您可以拥有任何时间或日期并从中提取所需的任何内容,只需将所需的组件添加到NSDateComponents对象中即可。

这是您可以从 NSDate 对象中提取的所有内容:http: //developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSDateComponents_Class/Reference/Reference.html

于 2013-08-14T18:29:09.977 回答
2

给你

NSString *string = @"00:50";
NSArray *components = [string componentsSeparatedByString:@":"];

int hours = [[components objectAtIndex:0] intValue];
int minutes = [[components objectAtIndex:1] intValue];

if( ((hours * 60) + minutes) > 30) {
     // ...
}
于 2013-08-14T18:25:35.567 回答