2013-04-16 78 views
0

在IOS在這裏我想添加2或3個小時的日期,在這裏我已經嘗試過,但我沒有得到正確的結果,請任何人請幫助我。IOS添加小時到datepicker

NSDate *mydate = birthDatePicker.date; 
    NSTimeInterval secondsInEightHours = 2 * 60 * 60; 
    NSDate *dateEightHoursAhead = [mydate dateByAddingTimeInterval:secondsInEightHours]; 
    NSLog(@"dateEightHoursAhead %@",dateEightHoursAhead); 
+0

我想你想的時間間隔爲2或3 hr.Am我吧? –

+0

正如我檢查你的代碼在我的最後工作正常。我可以加2小時mydate。 –

+0

@dineshprasanna:讓我看看你的結果? – Balu

回答

0

使用此代碼爲日期選擇器

- (IBAction)dobbtn:(id)sender { 


    UIActionSheet *dateActionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Done", nil]; 
    [dateActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    [dateActionSheet showInView:self.view]; 
    [dateActionSheet setFrame:CGRectMake(0, 200, 320, 383)]; 

    UIDatePicker *dp = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 80, 100, 116)]; 


    dp.datePickerMode = UIDatePickerModeDate; 

    [ dp addTarget: self action: 
    @selector(dateChanged:) 
    forControlEvents:UIControlEventValueChanged 
    ]; 


    [ self.view addSubview: dp ]; 

    txtDob.font = [ UIFont fontWithName: @"Arial" size: 22.0 ]; 
    txtDob.textColor = [ UIColor blueColor ]; 

    txtDob.text = [ dp.date description ]; 

    [dp setDatePickerMode:UIDatePickerModeDate]; 


    [dateActionSheet addSubview:dp]; 

} 



-(void) dateChanged: (id)sender 
{ 
    UIDatePicker *control = (UIDatePicker *) sender; 
    NSDate *selectedDate = control.date; 
    txtDob.text = [ selectedDate description ]; 
}