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

iOS

Using NSDate create a date object that is 10 days from today and call it 'futureDate'.

Hi guys,

I seem to be struggling with setting the date to 10 days ahead. Code below:

NSTimeInterval secondsPerDay = 60 * 60 * 24; NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

I've tried altering the values for 9 and 10 days ahead but I keep getting the incorrect error. I know I'm missing something really simple here so any help would be great.

Thanks,

Ashley

5 Answers

Hey Ashley,

You need to alloc/init NSDate *futureDate first. Then, you need to multiply secondsPerDay by 10 to get a value for 10 days.

The following code works :

NSTimeInterval secondsForTenDays = 60 * 60 * 24 * 10; NSDate *futureDate = [[NSDate alloc] init]; futureDate = [NSDate dateWithTimeIntervalSinceNow:secondsForTenDays];

Sebastien

Why can't you just format with the convenience constructor as stated in the video - dateWithTimeIntervalSinceNow: ?? It saves you a whole line.

The code below should validate in the Challenge, but throws an error.

NSTimeInterval *oneDaySeconds = (60 * 60 * 24); NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:(oneDaySeconds * 10)];

I love the videos, but the error-checking in the challenges has been wrong a few times and it's frustrating!

This worked for me, just one line of code -

NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:+(secondsPerDay * 10)];

Thanks Sebastien!

I tried the alloc init as well but I still seemed to get this wrong. Thanks for your help!

If you copy/paste the code I wrote, it will work fine :)

NSTimeInterval secondsForTenDays = 60 * 60 * 24 * 10; NSDate *futureDate = [[NSDate alloc] init]; futureDate = [NSDate dateWithTimeIntervalSinceNow:secondsForTenDays];

Hi Sebastien, sorry to bother you again.

Can you please help me solve the below? I seem to forget how to convert an object to a string.

Convert the 'futureDate' object to a string object named 'dateString'.

Thanks for your help again.

Ashley

Yeah it worked perfectly, thanks!

I don't know if this coding challenge has a bug or if I'm having an off day but I don't seem to be passing this specific challenge at all. On the next task it says:

Create a date format using the given 'dateFormatter' variable to display any date with the format of '02/29/2012'. Don't worry about using it for now, just set up the formatter.

So I've entered the following code which is not working:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM/DD/YYYY"];

I've had a look at the supporting document but I can't seem to figure out what I'm doing wrong.

Solved it, just removed the NSDateFormatter and * from the beginning. I don't know why I'm not getting this today! :)