我有三个文本字段,其中我想显示当前时间。在第一个文本字段中,它仅显示小时数,在第二个文本字段中仅显示分钟,在最后一个文本字段中显示上午或下午。为了获得当前时间,我使用此代码,但如何根据我的要求显示? 代码......
- (void)viewDidLoad {
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"HH:MM"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(@"%@",str);
//Set in the lable
time_label.text=[NSString stringWithFormat:@"%@ AM",str];
[super viewDidLoad];
}
提前致谢...
答案 0 :(得分:1)
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"hh MM a"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(@"%@",str);
NSMutableArray *Arr=[str componentsSeparatedByString:@" "];
textFeild1.text=[Arr objectAtIndex:0]; //hoursTextFeild
textFeild2.text=[Arr objectAtIndex:1]; //minutesTextFeild
textFeild3.text=[Arr objectAtIndex:2];//Am/Pm TextFeild
答案 1 :(得分:0)
- (void)viewDidLoad {
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"HH:MM"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(@"%@",str);
//Set in the label
//seperate its components
NSMutableArray *array = [str componentsSeperatedByString:@" "];
[firstTextField setText:[array objectAtIndex:0]];
[secondTextField setText:[array objectAtIndex:1]];
[thirdTextField setText:@"AM/PM"];
[super viewDidLoad];
}