Q:

Issue with datepicker in ios xcode project objective c off by a day

I have a program that works fine with datepicker except when I go forward to print days in which I will be alive. It skips a day so goes 0 on birthday 0 next day and then one. When I go backwards for days alive it goes 0 1 2 ect. Can anyone help with where I went wrong? To clarify it is a simple program in a beginner level class with 2 buttons. one makes an alert box with the day of the week. The other does the same for how many days you have passed or need to pass until you will be born.

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
- (IBAction)tapBtn:(id)sender;
- (IBAction)aliveBtn:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
  // day of the week
- (IBAction)tapBtn:(id)sender {
    NSDate *date = [self.datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE"];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"The day is" message:[dateFormatter stringFromDate:date] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
    [alertView show];
}
  //days alive or will be alive from selected birthday
- (IBAction)aliveBtn:(id)sender {
        NSDate *date1 = [NSDate date];
    NSDate *date2 = [self.datePicker date];

    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

    int numberOfDays = secondsBetween / 86400;

    if(numberOfDays < 0){
        numberOfDays *= -1;
        NSLog(@"You have been alive %d days.", numberOfDays);
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Days alive" message:[NSString stringWithFormat:@"You have been alive %d days.",numberOfDays] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [alertView show];
    }else{
    NSLog(@"you will be alive in %d days.", numberOfDays);
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Days will be alive in" message:[NSString stringWithFormat:@"You will be alive in %d days.",numberOfDays] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [alertView show];
        }

}
@end

A:

Okay! So the problem you are seeing is when you move the date picker forward exactly ONE day, your aliveBtn: method returns 0 days until the user will be born, correct? While debugging, did you try moving the minutes forward by one? It would make the aliveBtn: method return 1 day until birth.
So, this problem stems from the math you do in aliveBtn:
int numberOfDays = secondsBetween / 86400;
Just between the time I moved the day forward, secondsBetween was equal to 86394.30585...
The problem is that you declared numberOfDays as an int. An integer can only be whole numbers; so computing 86394.30585/86400 is equal to 0.99999 which in turn, since we are speaking in integers only, becomes 0.
To fix this problem, we use one of the math functions from C called ceil(double). Simply changing
int numberOfDays = secondsBetween / 86400;
to
int numberOfDays = ceil(secondsBetween / 86400);
Will fix your problem.
Good luck, I hope this fixes your problem. If it does, please mark this answer as accepted :)

