programmatically setting DatePicker time to current time
setDate:
method is used for setting specific date
[yourDatePicker setDate:[NSDate date]];
It will set picker date and time to current date and time.
Try this,
ViewController.h
@interface ViewController : UIViewController
{
IBOutlet UISwitch *onOffSwitch;
UIDatePicker *myPicker;
}
-(IBAction)onOffSwitch:(id)sender;
@end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,250,0,0);
myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[self.view addSubview:myPicker];
[myPicker release];
}
-(IBAction)onOffSwitch:(id)sender{
if(onOffSwitch.on) {
NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[myPicker setDate:date];
}
else
{
[myPicker setDate:[NSDate date]];
}
}