0

在 IOS 中,我想在日期中添加 2 或 3 个小时,在这里我尝试过但我没有得到正确的结果,有人可以帮助我吗?

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

1 回答 1

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 ];
}
于 2013-04-16T06:54:04.933 回答