I have NSString
in 24h format like that:
NSString *a = @"10:00";
NSString *b = @"23:59";
How can I get NSDate
with NSString
b?
I using:
+ setDateFormat: @"HH:mm"
==> It's work both for a and b when phone setting is 24h.
+ setDateFormat: @"hh:mm"
==> It's only work for a when phone setting is am/pm.
My full code, with self.timeInit is NSString with 24h format:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *date = [dateFormat dateFromString:self.timeInit];
if (!date){
[dateFormat setDateFormat:@"hh:mm"];
date = [dateFormat dateFromString:self.timeInit];
NSLog(@"Date 2: %@", date);
}
if (!date){
[dateFormat setDateFormat:@"H:mm"];
date = [dateFormat dateFromString:self.timeInit];
NSLog(@"Date 3: %@", date);
}
NSLog(@"Date: %@", date);
if (date){
[self.timePickerUI setDate:date];
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…