Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
anonymous1
164 PointsHelp using NSDate to add on some time and compare it to now?
I have been playing with this for almost 24 hours now. I am trying to add on some time to the createAt date of an entry and then see if it is earlier or later than NOW.
I've cleaned up my code and removed all the code that wasn't working. If anyone could help me in the right direction I would really appreciate it :)
Thanks
////////////////////// We need to work out how long the user will be at the location //////////////////////////////
// 1. get the createdAt time of the entry in parse.com
// Our time format for createdAt is 2014-04-07 22:45:17 +0000
NSDate *createdTime = [[objects objectAtIndex:0] createdAt];
NSLog(@"Created time is: %@", createdTime );
// 2. get the user submitted eta (how long they will be there) time of the entry
// this comes out in the format 03:30 am (we need to ignore the am / pm.
NSDate *etaTime = [[objects objectAtIndex:0] objectForKey:@"roughteta"];
NSLog(@"ETA time is: %@", etaTime );
// 3. grab the createdAt time that we now have and add on the etatime to get an 'endtime'
// We just want to add on the time, eg in this instance we want to add on 3 hours and 30 mins from the createdAt time
// 4. do a check to see if the endtime is later than NOW. If it is, show the end time in a nice format e.g. 10:30pm
// 5. if the endtime is earlier than now then say 'no status'
// FINALLY let's put the nice looking endTime into here for use in uitable
// leavingTime = ;
1 Answer
Christopher Hall
9,052 PointsHello Fox,
It looks like your ETA time is an estimated length in hours and minutes. You will need to first convert that into an NSTimeInterval which is measured in seconds. Then you can get the estimated date/time by calling the dateWithTimeInterval:sinceDate: method of NSDate like so. Then get the current date/time and measure it against your ETA date time to find the time interval. If the resulting NSTimeInterval is negative, that means the ETA time is earlier than the current date/time; vice versa if it is positive. Finally, to format the date for presentation, use the NSDateFormatter class. I would also look into NSLocale or NSTimeZone to get more accurate results if that is needed for your app. Otherwise dates could be off since NSDate is typically stored in the GMT+0 time zone.
NSDate *etaDateTime = [NSDate dateWithTimeInterval:etaTimeInterval sinceDate:createdTime];
NSDate *nowTime = [NSDate date];
NSTimeInterval *difference = [etaDateTime timeIntervalSinceDate:nowTime];