Time Since

Discussion in 'Mac Programming' started by forrestgrant, Jun 27, 2008.

  1. macrumors member

    Joined:
    Jun 24, 2008
    #1
    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];
    
     
  2. macrumors 6502a

    Joined:
    Mar 13, 2008
    #2
    ......and? What did it do? What errors did you get? How many? What were they pointing at?
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    You probably want to look at the 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 method...
     
  4. thread starter macrumors member

    Joined:
    Jun 24, 2008
    #4
    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?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    I just told you. It's really quite simple!
     
  6. thread starter macrumors member

    Joined:
    Jun 24, 2008
    #6
    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];
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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?
     
  8. thread starter macrumors member

    Joined:
    Jun 24, 2008
    #8
    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.
     
  9. macrumors 6502a

    Joined:
    Mar 13, 2008
    #9
    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.
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    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];
    
     
  11. macrumors 6502a

    Joined:
    Mar 13, 2008
    #11
    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?
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    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...
     
  13. macrumors 6502a

    Joined:
    Mar 13, 2008
    #13
    ok. ONE more question. How do you make each whole number equal a minute, hour, day, etc.?
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    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?
     
  15. macrumors 6502a

    Joined:
    Mar 13, 2008
    #15
    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
     

Share This Page