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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:01:22+00:00 2026-06-01T08:01:22+00:00

I am trying to create an app that calculates the time difference and the

  • 0

I am trying to create an app that calculates the time difference and the amount is multiplied by an amount in money. It is targeted to calculate in R$ (brazilian real) the amount that someone will have to pay for using a service during the time calculated by the app.

Here´s is my code:

- (IBAction)encerrar:(id)sender {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"HH:mm";
    NSString *temp = inicio.text;
    NSDate *then = [dateFormatter dateFromString:temp];
    NSDate *now = [NSDate date];
    NSTimeInterval timeInterval = [now timeIntervalSinceDate:then];
    NSString *ext = [dateFormatter stringFromDate:now];
    fim.text = ext;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString = [dateFormatter stringFromDate:timerDate];
    duracao.text = timeString;

    double timeIntervalInHours = (timeInterval / 3600.0) * 5;        

    NSString *vTotal = [[NSString alloc] initWithFormat:@"R$ %.2f", fecha];
    NSLog(@"%.2f", timeIntervalInHours);
    vlrTotal.text = vTotal;
}

The fact is that when we click on Calculate button, and if the duracao (duration) is equal to 1h it gives me the correct amount which is R$ 5,00. But when the duration is equal to 30mins or other value different from exact 1h, it gives the wrong amount.

I.E.: 1h should be R$ 5,00; while 1:30h should be R$ 7,50, but shows me R$ 6,50.

So, anyone could help me on this???

Thanks in advance!!!

  • 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-01T08:01:24+00:00Added an answer on June 1, 2026 at 8:01 am

    timeInterval is the amount of time in seconds, so if you want it in hours and fractions of an hour just do this:

    double timeIntervalInHours = timeInterval / 3600.0;
    

    and then multiple timeIntervalInHours times the price/hour to get the cost.

    EDIT
    Based on our chat, I would create a button with “iniciar” (start) as the title. When they press that button, I would store the current time and change the title to “encerrar” (stop). (I hope that my translations are correct, lol) Then the button action would look something like this:

    // self.startTime is a NSDate.
    - (IBAction)iniciar_encerrar:(UIButton *)sender {
        if ([sender.titleLabel.text isEqualToString:@"iniciar"]) {
            // We are starting the time
            if (self.startTime != nil) {
                return;
            }
    
            self.startTime = [NSDate date];
            [sender setTitle:@"encerrar" forState:UIControlStateNormal];
        } else {
            // We are stopping the time
            NSDate *currentTime = [NSDate date];
            NSTimeInterval elapsedTimeInSeconds = [currentTime timeIntervalSinceDate:self.startTime];
            double cost = elapsedTimeInSeconds / 3600.0 * 5.0;
            NSLog(@"%.2lf", cost);
    
            // reset the button
            self.startTime = nil;
            [sender setTitle:@"iniciar" forState:UIControlStateNormal];
        }
    }
    

    startTime is declared as follows:

    In your .h file, along with the other declared properties, add:

    @property (strong, nonatomic) NSDate *startTime;
    

    In your .m file, add this at the top with the other ones:

    @synthesize startTime;
    

    In your viewDidUnload function (in your .m file) add this:

    startTime = nil;
    

    This just gives you a place to store the startTime date.

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

Sidebar

Related Questions

I am trying to create an app that allows you to go to a
I'm trying to create an app that multiple users would log into a server
I am trying to create a simple app that displays a list of items
I'm trying to create a WinForms app that takes a screenshot on a set
So I'm trying to create an iphone app that does simple temperature conversion and
I am trying to create an iPad app that reads a video on a
I'm trying to create an app for Android that simply sends a command to
I'm trying to create a C# form app that will allow me to use
I'm trying to create a rails web app that does not use ActiveRecord framework
I'm trying to create an app that allows the user to open the android

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.