Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Amount of days between two dates

corneliussencorneliussen Posts: 21Registered Users
edited December 2010 in iPhone SDK Development
Hi

I am quite new to objective-c and need some help...

If there a easy way to figure our the amount of days between two dates? Or rephrased - what is the best way to do this.

Best regards
S
Post edited by corneliussen on

Replies

  • rames44rames44 Posts: 365Tutorial Authors, Registered Users @ @
    edited September 2008
    If you get the two dates each represented as an NSDate, you can use
       NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];
    

    NSTimeInterval is the elapsed time in seconds (expressed as a floating point number). You could then divide by 86400, which is the number of seconds in a day and round to the nearest integer.

    If you need to build an NSDate, you can use NSCalendar and NSDateComponents.

    Remember that, in order to make it come out even, you may need to make sure that the two NSDate's each represent approximately the same time of day on the particular date in question. Thus, if you're trying to say "how many days from date X to now," you may have to take the current date and time that [NSDate date] provides you, decompose it into NSDateComponents, zero out the hours, minutes, seconds and then rebuild it into an NSDate. If you don't do this, and if the first date/time is early in the morning and the second date/time is late at night, you may chalk up an extra day.
    For a little fun, check out my <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=298866985&mt=8" target="_blank">Biorhythms</a> app
  • HoofSCHoofSC Posts: 41Registered Users
    edited February 2009
    rames44 wrote: »
    Remember that, in order to make it come out even, you may need to make sure that the two NSDate's each represent approximately the same time of day on the particular date in question. Thus, if you're trying to say "how many days from date X to now," you may have to take the current date and time that [NSDate date] provides you, decompose it into NSDateComponents, zero out the hours, minutes, seconds and then rebuild it into an NSDate. If you don't do this, and if the first date/time is early in the morning and the second date/time is late at night, you may chalk up an extra day.

    Could anyone elaborate?

    I am having trouble with calculating the number of day CHANGES since the last date the app was opened. For example, if the date saved (in a sqlite field as DOUBLE) when the app last terminated is, say "Friday at 8:30pm", and then I next open the app and compare [NSDate date] -- say "Saturday, 12:01am" -- to my stored NSDate using the following:
    -(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today {
    	NSDate *startDate = lastDate;
    	NSDate *endDate = today;
    	NSCalendar *gregorian = [[NSCalendar alloc]
    							 initWithCalendarIdentifier:NSGregorianCalendar];
    	unsigned int unitFlags = NSDayCalendarUnit;
    	NSDateComponents *components = [gregorian components:unitFlags
    												fromDate:startDate
    												  toDate:endDate options:0];
    	int days = [components day];
    	return days;
    }
    

    the return value is still 0. It didn't register the change in calendar date as a day...

    Is there something simple I'm missing, or can add, or is this a far more complex operation?
  • RickMaddyRickMaddy Posts: 2,122New Users
    edited February 2009
    There's an easier way. If you have two dates then the difference can be found this way:
    NSDate *lastDate = ...
    NSDate *todaysDate = ...
    NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
    NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
    NSTimeInterval dateDiff = [lastDiff - todaysDiff];
    

    At this point 'dateDiff' is equal to the number of seconds between the two dates. You should now be able to convert the number of seconds to minutes, hours, and/or days as needed (divide by 60, 3600, 86400 respectively).
  • zulfishahzulfishah Posts: 203Registered Users @ @
    edited May 2009
    This is useful. But one thing related to this that i'm confused about is handling daylight savings. If you compare 2 dates(e.g today and 2 months ago), the number of seconds returned in the diff will contain an extra hour because of the DST change, right? So it won't be possible to keep the exact time. For e.g. if i set a monthly reminder at 1am on 01/01/2009, in May 01 2009 it'll show up at 2am. How should I compensate for DST? Is there some library in the OS docs? I've looked at NSDate and NSCalendar and they don't say anything about DST.

    RickMaddy wrote: »
    There's an easier way. If you have two dates then the difference can be found this way:
    NSDate *lastDate = ...
    NSDate *todaysDate = ...
    NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
    NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
    NSTimeInterval dateDiff = [lastDiff - todaysDiff];
    

    At this point 'dateDiff' is equal to the number of seconds between the two dates. You should now be able to convert the number of seconds to minutes, hours, and/or days as needed (divide by 60, 3600, 86400 respectively).
    Check out my apps:<br />
    <br />
    <a href="http://appsto.re/contactsjournal" target="_blank"> Contacts Journal</a><br />
    <br />
    <a href="http://appsto.re/ContactsJournalLite" target="_blank"> Contacts Journal Lite</a><br />
    <br />
    <a href="http://itun
  • tawpietawpie Posts: 348Registered Users
    edited May 2009
    no library that I know of for DST. And have fun dealing with leap years as well.

    I do date arithmetic in my app, and basically had to code the thing by hand, referencing things back to a starting point (10/15/1582?) and then computing days since then. Google will find you some good stuff on dealing with leap years, and likewise you'll most likely have to modify RickMaddy's suggestion for DST. Don't forget that AZ and HI and scattered places around the world (equatorial, parts of AUS and CAN, China and JPN to mention a few) don't do DST so you'll need location as well. Gonna be a pain!
    "Hardware <u>will</u> break. Software comes broken" Unknown<br />
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=294438793&mt=8" target="_blank">Calc-12E</a> <-- ditch your old calculator.<br />
    <a href="htt
  • mjewmjew Posts: 1New Users
    edited December 2010
    zulfishah wrote: »
    This is useful. But one thing related to this that i'm confused about is handling daylight savings. If you compare 2 dates(e.g today and 2 months ago), the number of seconds returned in the diff will contain an extra hour because of the DST change, right? So it won't be possible to keep the exact time. For e.g. if i set a monthly reminder at 1am on 01/01/2009, in May 01 2009 it'll show up at 2am. How should I compensate for DST? Is there some library in the OS docs? I've looked at NSDate and NSCalendar and they don't say anything about DST.

    You are thinking about it wrong.
    An NSDate object expresses time as a continuous flow of seconds.
    NSDate has no affiliation with any specific calendar or location. The concept of Daylight Savings Time is a calendar-specific issue. The flow of time in the real universe doesn't "skip" an hour twice a year. Time just flows, one second after the other. Therefore, you can easily add and subtract time (expressed in seconds) when you use NSDate objects.

    If you want to display an NSDate as an understandable date for us humans, you then choose a calendaring system (expressed in the NSCalendar object) and have the two objects work together to show the given point in time as a calendar date.

    Note, though, that when you do this you are taking a single NSDate (as a point in the flow of time) and expressing it as a single calendar date.

    Then, if you have a second NSDate, you can express the second one as a single calendar date. There is no problem with discontinuity because each NSDate is a single point in time, and each one has a consistent expression in a given NSCalendar.

    That's why they separated NSDates and NSCalendars (killing off the NSCalendarDate object). It makes the calculations involving time (which has no discontinuities or complications) separate from the issue of calendaring (which has all sorts of complications and discontinuities).

    For another example of calendaring complexity and discontinuity, what does one do with dates that span the end of the use of the Julian Calendar and the introduction of the Gregorian Calendar (when the date jumped from 4 October 1582 to 15 October 1582 in one day)?
Sign In or Register to comment.