Jonathan Gurebo
Jonathan Gurebo

Reputation: 285

Xcode - NSTimeInterval Years double value, how to get MORE decimals?

i wan't to know exactly how many years it is between 2 NSDate's. (current Date and Date picker date)

i'm using NSTimeInterval (seconds) how to make it to years?

This code will make the value to Years:

NSTimeInterval distanceBetweenDates = [now timeIntervalSinceDate:date];

double secondsInAnYear = 31536000;
double YearsBetweenDates = distanceBetweenDates / secondsInAnYear;

NSString *dateString = [NSString stringWithFormat:@"%f", YearsBetweenDates];

labelView.text = dateString;

but i just get 6 decimals!

i want more than 6 decimals. How?

Upvotes: 0

Views: 1038

Answers (2)

Johan Kool
Johan Kool

Reputation: 15927

Take a look at -[NSCalendar (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts]. This does the calculation correctly. You can't assume a year always is exactly 31536000 seconds (leap year, or even those leap second(s) that get added occasionally).

Upvotes: 1

Attila H
Attila H

Reputation: 3606

Have you tried using %lf which is the designated specifier for long float/double values?

NSString *dateString = [NSString stringWithFormat:@"%lf", YearsBetweenDate];

Upvotes: 0

Related Questions