我正在使用UIDate选择器并且正常使用它。我使用它只是为了让时间不是约会。我在标签上得到的时间,但当我将此时间字符串转换为NSDate类型时,它显示我错误的输出。显示的输出如下。 1999-12-31 23:30:00 但应该是06:45 PM。任何人都可以找到我的错误。
我使用的代码如下。
- (void)showPicker:(NSDate *)date
{
actionsheet = [[UIActionSheet alloc] initWithTitle:@"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
pickdate.datePickerMode = UIDatePickerModeTime;
pickdate.timeZone = [NSTimeZone localTimeZone];
[pickdate addTarget:self action:@selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
[pickdate setDate:date animated: YES];
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(@"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
[df setDateFormat:@"hh:mm a"];
timelbl.text = [NSString stringWithFormat:@"%@",[df stringFromDate:pickdate.date]];
NSLog(@"Picker MOde IS %@",df );
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (toggleswitch.on)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"hh:mm a"]; // depends on 12(hh) or 24(HH)
yourDate = [dateFormatter dateFromString:timelbl.text];
[self showPicker:yourDate];
NSLog(@"YOUR DATE: %@", yourDate);
}
}
答案 0 :(得分:0)
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSString *theDate = [dateFormat stringFromDate:date];
NSString *theTime = [timeFormat stringFromDate:date];
NSLog(@"%@:%@",theDate,theTime);