Question

I have a label and two arrow buttons. When I push on the left arrow I want to show one day less than that date at the moment. And when I push on the right arrow a day should be added.

At the moment this is my code.

- (IBAction)addDay:(id)sender {
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = 1;
    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    NSString *dateNow = _lblDate.text;

    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EE. d MMMM YYYY"];
    NSDate *date = [dateFormat dateFromString:dateNow];

    date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0];
    NSString *dateString = [dateFormat stringFromDate:date];
    _lblDate.text = dateString;

}

- (IBAction)deleteDay:(id)sender {

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = 1;
    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    NSString *dateNow = _lblDate.text;

    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EE. d MMMM YYYY"];
    NSDate *date = [dateFormat dateFromString:dateNow];

    date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0];
    NSString *dateString = [dateFormat stringFromDate:date];
    _lblDate.text = dateString;

}

But for some reason or another the dates are wrong. There is no pattern when I'm adding and deleting dates.

Can anybody help me ?

Kind regards

Was it helpful?

Solution

Your code for addition is correct. However relying on string to date conversion is creating problems. I'll advise you to take a NSDate object as instance variable and then update this instance variable on going to next or previous day. After updating value you can use dateformatter to retrieve string from this date. Right now when you use your label's text to generate date, it creates problems. So that's how you can do it

// currentDate is my instance variable.
currentDate = [[NSDate date] retain];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
dateLabel.text = [dateFormat stringFromDate:currentDate];

And that's how your nextdayfunctionwill look Like

   (IBAction)nextDay:(id)sender {

       NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
       dayComponent.day = 1;
       NSCalendar *theCalendar = [NSCalendar currentCalendar];

       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
       [dateFormat setDateFormat:@"EE. d MMMM YYYY"];

       currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain];
       NSString *dateString = [dateFormat stringFromDate:currentDate];
       dateLabel.text = dateString;
    }

And here is previous day

enter code here

    (IBAction)previousDay:(id)sender {

        NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
        dayComponent.day = -1;
        NSCalendar *theCalendar = [NSCalendar currentCalendar];

        NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormat setDateFormat:@"EE. d MMMM YYYY"];

        currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain];
        NSString *dateString = [dateFormat stringFromDate:currentDate];
        dateLabel.text = dateString;
    }

OTHER TIPS

I'm not sure this is the main issue, but in - (IBAction)deleteDay:(id)sender, dayComponent.day = 1; should be dayComponent.day = -1;.

I see two issues with this.

  1. Not actually decrementing the day by one (looks like addition?)
  2. Not updating the UI on the main thread.

Try making these changes:

- (IBAction)deleteDay:(id)sender {

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = -1;
    /* snip */
}

And...

[_lblDate performSelectorOnMainThread:@selector(setText:) withObject:dateString waitUntilDone:NO];

To add a day you need to addTimeInterval of one day as follows

NSDate *newDate1 = [date addTimeInterval:60*60*24];

To subtract a day do following

NSDate *newDate2 = [date addTimeInterval:-60*60*24];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top