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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:48:28+00:00 2026-05-26T12:48:28+00:00

When I try to calculate the time elapsed using Date Components the date is

  • 0

When I try to calculate the time elapsed using Date Components the date is one second off. Why is this?

NSCalendar *sysCalendar = [NSCalendar currentCalendar];
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:timeStarted toDate:[[NSDate date]addTimeInterval:+1.0] options:0];
timeElapsed = [sysCalendar dateFromComponents:conversionInfo];

My temporary fix is to add one second, but I want to understand why it is happening or if there is a better fix.

EDIT: It is not one second off, but it is rounding down. When I get the time interval in seconds (which brings it down to decimal places) it comes out to be 1.9, 2.9, 3.9 and is showing 00:00:01, 00:00:02, 00:00:03 respectively. How can I get it to round up?

  • 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-26T12:48:28+00:00Added an answer on May 26, 2026 at 12:48 pm

    You can round a double (NSTimeInterval) to an integer with:

    NSTimeInterval unrounded = 7.8;
    long rounded = lround(unrounded);
    

    The long integer ’rounded’ will now contain 8. You can now cast this back to a time interval.

    NSTimeInterval roundedTimeInterval = (NSTimeInterval)rounded;
    

    and use that with the NSDate functions.

    So to create an NSDate with only full seconds you can use:

    NSDate *nowEnough = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)lround([NSDate timeIntervalSinceReferenceDate])];
    

    or to round off an existing NSDate’s subseconds:

    NSDate *thenEnough = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)lround([existingNSDatesName timeIntervalSinceReferenceDate])];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the difference between two date/time in java using Date and
I try to calculate a List with times. But using LocalTime from Joda Time
I want to calculate downloading time in my ftp download manager. I am using
I need to calculate changes off of time series of nullable numbers. The following
I have created this little program to calculate pi using probability and ratios. In
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
try it out: volatile float bob = -344.0f; unsigned int fred = (unsigned int)bob;
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
This is a problem I've been racking my brains on for a long time,
I am trying to write a class that will calculate checksums using multiple processes,

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.