PDA

View Full Version : Time Since




forrestgrant
Jun 27, 2008, 02:55 PM
hey everyone.
I am trying to figure out the time since a given date. I set up my date object as:

NSDate *beginDate = [NSDate dateWithNaturalLanguageString:@"07/17/07"];

Then I basically want to figure out how many (hours, days months) since that date have passed. All seperately of course. Something like:

hoursSince = [NSDate hoursSince:beginDate];
minutesSince = [NSDate minutesSince:beginDate];
secondsSince = [NSDate secondsSince:beginDate];



liptonlover
Jun 27, 2008, 02:58 PM
......and? What did it do? What errors did you get? How many? What were they pointing at?

robbieduncan
Jun 27, 2008, 02:58 PM
You probably want to look at the timeIntervalSinceDate: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/timeIntervalSinceDate:) method. So take your date, create another NSDate which is the time right now, compare them with that and then convert (NSTimeIntervals are always in seconds). Easy :D

Ooh, even easier use the timeIntervalSinceNow (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/timeIntervalSinceNow) method...

forrestgrant
Jun 27, 2008, 02:59 PM
......and? What did it do? What errors did you get? How many? What were they pointing at?

I havent done that yet... I was just saying something like that SHOULD exist... since it does not, how would I go about doing it?

robbieduncan
Jun 27, 2008, 03:00 PM
I havent done that yet... I was just saying something like that SHOULD exist... since it does not, how would I go about doing it?

I just told you. It's really quite simple!

forrestgrant
Jun 27, 2008, 03:06 PM
This is what I assume it should be... But it does not work

NSDate *previousDate = [NSDate dateWithNaturalLanguageString:@"07/17/07"];
NSTimeInterval *since = [previousDate timeIntervalSinceNow];

robbieduncan
Jun 27, 2008, 03:08 PM
NSTimeIntervals are not objects: they are simply typedefed to double. No need to make the since variable a pointer.


NSDate *previousDate = [NSDate dateWithNaturalLanguageString:@"07/17/07"];
NSTimeInterval since = [previousDate timeIntervalSinceNow];


should work. But in all seriousness could you not tell that from the documentation built into XCode?

forrestgrant
Jun 27, 2008, 03:14 PM
that worked.... Thanks...
I am a ruby/rails/php guy... I have been working with xcode and objective c for about 24 hours now so its mostly cut and paste thus far.

Slowly getting the hang of it though... thanks.

liptonlover
Jun 27, 2008, 04:58 PM
thanks for posting this... I didn't post but I'm interested in this too. But now I have my own question to add on to this if you don't mind:
How can I convert the time interval it returns into an int? The main idea behind this would be for games that go by real time, updating even when the app isn't open. So when you find out how long the user has been gone, you convert it into an int, and run a loop X times until it's done enough updating.

robbieduncan
Jun 27, 2008, 05:00 PM
An int of what? The number returned is the number of seconds difference at millisecond precession (it's a floating point number). Assuming the number of seconds is not too many to cause an overflow you can simply cast it to an int via something like


int noSeconds = (int) [previousDate timeIntervalSinceNow];

liptonlover
Jun 27, 2008, 05:03 PM
huh really? From my palm background I'm used to dates really just being a number, but since the Mac has a date object type, I figured it would return a date.

Just curious, would this work?

int timeinterval;
timeinterval=[previousDate timeIntervalSinceNow];

That should automatically make it an int you're dealing with right?

robbieduncan
Jun 27, 2008, 05:05 PM
I'd explicitly cast, but that will work with a compiler warning. A NSTimeInterval is just a double that says how many seconds the difference between the dates is...

liptonlover
Jun 27, 2008, 05:10 PM
ok. ONE more question. How do you make each whole number equal a minute, hour, day, etc.?

robbieduncan
Jun 27, 2008, 05:15 PM
ok. ONE more question. How do you make each whole number equal a minute, hour, day, etc.?

OK, if we assume the cast is good enough (you may loose part of a second, but that's not an issue surely? If it is use the ceiling function) then it's laughably easy. If you can't work out how to turn seconds into minutes etc are you sure you want to be programming? :p


int secondsDifference = (int) [previousDate timeIntervalSinceNow];
int seconds = secondsDifference%60; // Seconds over the last full minute
int minutes = secondsDifference%3600; // Seconds over the fill hour
int hours = secondsDifference/3600;


From there surely you can work the rest out?

liptonlover
Jun 27, 2008, 05:19 PM
lol I do miss obvious stuff like that at first... though I would have thought to do that eventually. But I meant a method. Thanks though! I'll stop stealing the thread now :D