Re: How to Display a Time Interval


Dave
 

Hi,

As far as I know I don’t have to worry about leap years, get the Interval between now and some future date, this includes all the leap years in between. I am displaying it at the moment by dividing by the number of seconds in a year, week, hours and minute which is accurate enough for this applications.

Here is a method to do it:


#define kLTWTimeSecondsPerMinute 60
#define kLTWTimeMinutesPerHour 60
#define kLTWTimeSecondsPerHour (kLTWTimeSecondsPerMinute * kLTWTimeMinutesPerHour)
#define kLTWTimeHoursPerDay 24
#define kLTWTimeDaysPerWeek 7
#define kLTWTimeWeeksPerYear 52

#define kLTWTimeSecondsPerDay (kLTWTimeSecondsPerHour * kLTWTimeHoursPerDay)
#define kLTWTimeSecondsPerWeek (kLTWTimeSecondsPerDay * kLTWTimeDaysPerWeek)
#define kLTWTimeSecondsPerYear (kLTWTimeSecondsPerWeek * kLTWTimeWeeksPerYear)


-(NSString*) newIntevalStringWithDateString:(NSString*) theDateString andTitleString:(NSString*) theTitleString
{
NSDateFormatter* myDateFormatter;
NSDate* myIntevalDate;
NSString* myIntevalString;
NSTimeInterval myWorkTimeInteval;
NSInteger myNumberOfYears;
NSInteger myNumberOfWeeks;
NSInteger myNumberOfDays;
NSInteger myNumberOfHours;
NSInteger myNumberOfMinutes;

myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd"];
myIntevalDate = [myDateFormatter dateFromString:theDateString];
myWorkTimeInteval = [myIntevalDate timeIntervalSinceNow];

myNumberOfYears = myWorkTimeInteval / kLTWTimeSecondsPerYear;
myWorkTimeInteval = myWorkTimeInteval - (myNumberOfYears * kLTWTimeSecondsPerYear);

myNumberOfWeeks = myWorkTimeInteval / kLTWTimeSecondsPerWeek;
myWorkTimeInteval = myWorkTimeInteval - (myNumberOfWeeks * kLTWTimeSecondsPerWeek);

myNumberOfDays = myWorkTimeInteval / kLTWTimeSecondsPerDay;
myWorkTimeInteval = myWorkTimeInteval - (myNumberOfDays * kLTWTimeSecondsPerDay);

myNumberOfHours = myWorkTimeInteval / kLTWTimeSecondsPerHour;
myWorkTimeInteval = myWorkTimeInteval - (myNumberOfHours * kLTWTimeSecondsPerHour);

myNumberOfMinutes = myWorkTimeInteval / kLTWTimeSecondsPerMinute;
myWorkTimeInteval = myWorkTimeInteval - (myNumberOfMinutes * kLTWTimeSecondsPerMinute);

myIntevalString = [[NSString alloc] initWithFormat:@"%@ - Years: %zd  Weeks: %2zd   Days: %zd   Hours: %zd   Minutes: %zd",theTitleString,myNumberOfYears,myNumberOfWeeks,myNumberOfDays,myNumberOfHours,myNumberOfMinutes];

return myIntevalString;
}

All the Best
Dave

On 19 May 2019, at 19:45, Marco S Hyman <marc@...> wrote:


I have the Time Interval between now and the future date in seconds, but I can’t seem to find any methods to display this in terms of years. months, weeks, days etc.

Can not be done accurately using only a Time Interval in seconds.  You also need either the start or end date to figure out leap year complications.

Is there a method or class somewhere that does this or do I need to roll my own?

Don’t know of any.  I’d probably roll my own.

Marc



Join cocoa@apple-dev.groups.io to automatically receive all group messages.