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

  • SEARCH
  • Home
  • 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 7688589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:57:23+00:00 2026-05-31T19:57:23+00:00

I’ve tried all the time calculating examples I found on this site but somehow

  • 0

I’ve tried all the time calculating examples I found on this site but somehow I’m getting 0 as value every time. I’m new to IOS and the NSDate is giving me a run for it’s money 🙂

I want to record time A when I press button “startStop”, and then record time B when I press it again. Pressing it a second time (deselecting) has to calculate the time elapsed between these 2 dates. So far I have this:

-(IBAction)buttonClick {


    NSDate *startStopDate =  [NSDate alloc];

    NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh:mm:ss"];

    NSString *currentTime = [[NSString alloc] init];
    NSString *currentTime2 = [[NSString alloc]init];


    NSDate *start =[ [NSDate alloc]init];
    NSDate *stop = [[NSDate alloc] init];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    if (startStop.selected==NO) {

        NSLog(@"started");
        [startStop setSelected:YES];

        startStopDate = [NSDate date];


        currentTime = [formatter stringFromDate:startStopDate];

        NSLog(@"Current timestarted is %@",currentTime);


        startTime.text = currentTime;

        start = [formatter dateFromString:currentTime];


    }

    else {


        NSLog(@"Selected");
        [startStop setSelected:NO];

        startStopDate = [NSDate date];

        currentTime2 = [formatter stringFromDate:startStopDate];


        NSLog(@"Current time is %@",currentTime2);

        stopTime.text = currentTime2;
        stop = [formatter dateFromString:currentTime2];


        NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

        NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                            fromDate:start
                                                              toDate:stop
                                                             options:0];



        NSInteger hours = [components hour];
        NSInteger minutes = [components minute];
        NSInteger seconds = [components second];


        NSLog(@"hello %d, %d, %d", hours, minutes, seconds);

    }
}

My labels give the correct hour:minute output when pressed, but I can’t seem to get them to transfer to the date calculation.
I’ve been trying numerous variations with dateFromString and stringFromDate and multiple date/string objects as you can see, but so far I only get big negative numbers or just zero as result. Any help appreciated 😉

Greets, Nick

  • 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-05-31T19:57:24+00:00Added an answer on May 31, 2026 at 7:57 pm

    That’s because start is a local variable. When the button is clicked the second time, your code will skip the if block and go straight to the else block, leaving the start variable allocated but not initialized to anything useful. You need to store this start value outside of a local context if you want to use it to calculate the difference between the start and stop times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.