Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:09:08+00:00 2026-06-15T03:09:08+00:00

I have a label and two arrow buttons. When I push on the left

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:09:10+00:00Added an answer on June 15, 2026 at 3:09 am

    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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a label that i want to show it's Text(1234567) Like A Number
I have two label elements. I want the first one to be shown all
I have two labels that are stacked. If I want a horizontal line between
I have a 'time select' form that is two select option elements, one for
i have an label and two panels. i want to add the label into
I have two view controller, ParentViewController have label that updated using SecondViewController . Now
I have Six labels, I want to hide two label's text randomly when a
this is a two part question that probbably have a similar answer. I want
I have a form which contains a 'gender' label and two radio buttons'male' and
If I have an array that looks like this; markers[56] = { label: One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.