views:

22

answers:

1

Hi

In my app I have next and previous week buttons, but also a UIDatePicker. So lets say I press the net week button, how can I adjust the day in the UIDatePicker to increase by 7 days? And visa versa for back 7 days.

Thanks!

+1  A: 

Use the dateByAddingTimeInterval: method of the NSDate class, and the setDate:animated: method of the UIDatePicker class, to adjust the picker's current date:

NSDate *currentDate = [datePicker date];

// Set the date 7 days earlier
NSDate *sevenDaysEarlier = [currentDate dateByAddingTimeInterval:-(60 * 60 * 24 * 7)];
[datePicker setDate:sevenDaysEarlier animated:YES];

// Set the date 7 days later
NSDate *sevenDaysLater = [currentDate dateByAddingTimeInterval:60 * 60 * 24 * 7];
[datePicker setDate:sevenDaysLater animated:YES];
BoltClock