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 4114586
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:28:27+00:00 2026-05-20T22:28:27+00:00

I need to create a Timer that counts down from a string I get

  • 0

I need to create a Timer that counts down from a string I get from the server. the string outputs total seconds left.

I am using this code – it calculates the initial number time right and it counts down but the minutes are the problem.

EDIT – Every real minute the minute calculation goes up by one. But I don’t understand why it is doing this.

-(void)showTimmer:(id) sender {

//get total amount of seconds

NSString *timeRaw =  [ArrayFromServer objectAtIndex:1];
NSArray *timeArr = [timeRaw componentsSeparatedByString:@","];
timetoInt =  [timeArr objectAtIndex:0];
int time1 = [timetoInt intValue];

//set the second countdown

NSDate* now = [NSDate date];    
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [gregorian components:(NSSecondCalendarUnit) fromDate:now];

NSInteger seconds = time1 - [dateComponents second];

[gregorian release];

// do the hours days maths

int hour = (seconds / 3600 );
seconds -= hour * 3600;
int minute = (seconds / 60 );
seconds -= minute * 60;
int second = seconds;

//set the labels

countdownLabel.text = [NSString stringWithFormat:@"%02dH %02dM %02ds", hour, minute, second];
  • 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-20T22:28:28+00:00Added an answer on May 20, 2026 at 10:28 pm

    You’re using your pliers to drive a screw. The seconds method of an NSDateComponents gives you what you would see on a clock if you looked at the second hand at the time represented by your NSDate. For example, as I type this, it’s UTC Saturday March 26, 2011, 02:52:04. If I got [NSDate date], turned it into an NSDateComponents and asked that for its seconds, I’d get “4”. If I did it again in a second, I’d get “5”, and when the minute clicked over, I’d get “59”, then “0”.

    You can verify this for yourself; put this in your timer method:

    NSInteger dcSecond = [dateComponents second];
    NSLog(@"%d", dcSecond);
    

    So every time the minute of the actual clock ticks over, your calculation of the remaining time:

    NSInteger seconds = time1 - [dateComponents second];
    

    subtracts 0 from time1 and gives you back time1. That’s why you’re having the problem.

    The way* to fix it is to dump the calendar stuff and use CFAbsoluteTimeGetCurrent(). First, put the start time in your timer’s userInfo (or in an ivar if you prefer):

    [NSTimer scheduledTimer...
            // The function returns CFAbsoluteTime, which is a
            // typedef'd double but you're already working with
            // integers, so use a cast
                 userInfo:[NSNumber numberWithInteger:(NSInteger)CFAbsoluteTimeGetCurrent()]
                    ...];
    

    Then change your calculation of the remaining time (the variable you call seconds:

    NSInteger startTime = [[timer userInfo] integerValue];
    NSInteger currTime = (NSInteger)CFAbsoluteTimeGetCurrent();
    NSInteger elapsedTime = currTime - startTime;
    NSInteger duration = time1 - startTime;
    //NSInteger seconds = time1 - [dateComponents second];
    NSInteger remaining =  duration - elapsedTime;
    
    int hour = remaining / 3600;
    ...
    

    *: Well, one way, but I think probably the best.

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

Sidebar

Related Questions

No related questions found

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.