stringWithFormat 的问题 [英] Issues with stringWithFormat
本文介绍了stringWithFormat 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请如何让我的 stringWithFormat/initWithFormat 显示 am/pm 而不是 24hrs,即使用户从 pickerviewer 中选择 am/pm?当用户选择时间时;例如 4:15 pm,而不是显示 4:15pm 作为确认,而是显示 16:15.我希望它是下午 4:15.请帮忙!!
Please how do I make my stringWithFormat/initWithFormat to display am/pm instead of 24hrs, even though users selects am/pm from the pickerviewer? When a user select time; eg 4:15 pm, instead of it to display 4:15pm as confirmation, it displays 16:15 instead. I want it as 4:15pm. PLEASE HELP!!
-(IBAction)datePickerValueChanged:(id)sender
{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"h:mm:ss a"];
NSString *strDate = [dateFormatter stringFromDate:self.pick.date];
dateArray = [strDate componentsSeparatedByString:@":"];
NSDate *current = [NSDate date];
NSString *currentTime = [dateFormatter stringFromDate:current];
NSArray *currArray = [currentTime componentsSeparatedByString:@":"];
curr = [currArray[0] intValue]*60 + [currArray[1] intValue];
min = [currArray[0] intValue]*60 + [currArray[1] intValue] + 30;
next = [[dateArray objectAtIndex:0] intValue]*60 +[[dateArray objectAtIndex:1] intValue];
....这是显示值的地方:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
time = [[NSString alloc]initWithFormat:@"%@T%@:%@:00",strDate, [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]];
NSString *msg = [[NSString alloc] initWithFormat:@"%@ %@ %@ %@ ?", NSLocalizedString(@"do_ticket", nil), self.category.description,NSLocalizedString(@"at", nil),[[NSString alloc]initWithFormat:@"%@:%@", [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]]];
alert_tick = [[UIAlertView alloc] initWithTitle:nil
message:msg
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Yes", nil), NSLocalizedString(@"No", nil), nil];
[alert_tick show];
}
推荐答案
改用 @"hh:mm a"
格式.
示例:
NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSDate *formattedDate = [formatter stringFromDate:today];
NSLog(@"date : %@", formattedDate);
输出:
date : 09:58 AM
这篇关于stringWithFormat 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文