解析时间 PT##H##M 到 NSDate
Parse Time PT##H##M to NSDate
我可以通过使用子字符串打破字符串来做到这一点,但应该有一些方法来解析特定格式的字符串并得到所需的值。
如果有任何其他简单而优雅的方法可以做到这一点,请帮助我。
[更新]
这就是我使用子字符串的方式:
NSRange ptRange = [timeString rangeOfString:@"T"];
NSRange hRange = [timeString rangeOfString:@"H"];
NSRange mRange = [timeString rangeOfString:@"M"];
NSString *hours = nil;
if (hRange.location != NSNotFound) {
hours = [timeString substringWithRange:NSMakeRange(ptRange.location+ptRange.length, hRange.location-(ptRange.location+ptRange.length))];
}
else {
hRange = ptRange;
}
NSString *minutes = nil;
if (mRange.location != NSNotFound) {
minutes = [timeString substringWithRange:NSMakeRange(hRange.location+hRange.length, mRange.location-(hRange.location+hRange.length))];
}
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSDate*date = [df dateFromString:[NSString stringWithFormat:@"%d:%d",hours.integerValue,minutes.integerValue]];
到目前为止,我的实现就是这个问题的答案
NSRange ptRange = [timeString rangeOfString:@"T"];
NSRange hRange = [timeString rangeOfString:@"H"];
NSRange mRange = [timeString rangeOfString:@"M"];
NSString *hours = nil;
if (hRange.location != NSNotFound) {
hours = [timeString substringWithRange:NSMakeRange(ptRange.location+ptRange.length, hRange.location-(ptRange.location+ptRange.length))];
}
else {
hRange = ptRange;
}
NSString *minutes = nil;
if (mRange.location != NSNotFound) {
minutes = [timeString substringWithRange:NSMakeRange(hRange.location+hRange.length, mRange.location-(hRange.location+hRange.length))];
}
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSDate*date = [df dateFromString:[NSString stringWithFormat:@"%d:%d",hours.integerValue,minutes.integerValue]];
我可以通过使用子字符串打破字符串来做到这一点,但应该有一些方法来解析特定格式的字符串并得到所需的值。
如果有任何其他简单而优雅的方法可以做到这一点,请帮助我。
[更新] 这就是我使用子字符串的方式:
NSRange ptRange = [timeString rangeOfString:@"T"];
NSRange hRange = [timeString rangeOfString:@"H"];
NSRange mRange = [timeString rangeOfString:@"M"];
NSString *hours = nil;
if (hRange.location != NSNotFound) {
hours = [timeString substringWithRange:NSMakeRange(ptRange.location+ptRange.length, hRange.location-(ptRange.location+ptRange.length))];
}
else {
hRange = ptRange;
}
NSString *minutes = nil;
if (mRange.location != NSNotFound) {
minutes = [timeString substringWithRange:NSMakeRange(hRange.location+hRange.length, mRange.location-(hRange.location+hRange.length))];
}
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSDate*date = [df dateFromString:[NSString stringWithFormat:@"%d:%d",hours.integerValue,minutes.integerValue]];
到目前为止,我的实现就是这个问题的答案
NSRange ptRange = [timeString rangeOfString:@"T"];
NSRange hRange = [timeString rangeOfString:@"H"];
NSRange mRange = [timeString rangeOfString:@"M"];
NSString *hours = nil;
if (hRange.location != NSNotFound) {
hours = [timeString substringWithRange:NSMakeRange(ptRange.location+ptRange.length, hRange.location-(ptRange.location+ptRange.length))];
}
else {
hRange = ptRange;
}
NSString *minutes = nil;
if (mRange.location != NSNotFound) {
minutes = [timeString substringWithRange:NSMakeRange(hRange.location+hRange.length, mRange.location-(hRange.location+hRange.length))];
}
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSDate*date = [df dateFromString:[NSString stringWithFormat:@"%d:%d",hours.integerValue,minutes.integerValue]];