Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

forrestgrant

macrumors member
Original poster
Jun 24, 2008
34
0
hey everyone.
I am trying to figure out the time since a given date. I set up my date object as:
Code:
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:
Code:
hoursSince = [NSDate hoursSince:beginDate];
minutesSince = [NSDate minutesSince:beginDate];
secondsSince = [NSDate secondsSince:beginDate];
 
......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?
 
This is what I assume it should be... But it does not work

Code:
	NSDate *previousDate = [NSDate dateWithNaturalLanguageString:@"07/17/07"];
	NSTimeInterval *since = [previousDate timeIntervalSinceNow];
 
NSTimeIntervals are not objects: they are simply typedefed to double. No need to make the since variable a pointer.

Code:
	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?
 
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.
 
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.
 
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

Code:
int noSeconds = (int) [previousDate timeIntervalSinceNow];
 
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?
 
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...
 
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

Code:
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?
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.