views:

65

answers:

1

I'm eiter a bit overwhelmed by the complexity of NSDate or I simply don't understand the concept of it :)

the only thing I want to do is to create an NSDate Instance representing todays date and a fix time of 20.00h respectively 8pm.

this can't be as difficult as creating an Instance holding the current time and afterwards either subtracting oder adding the neccessary difference to 8pm, can it?

thanks for your help...

the ultranoob

+3  A: 

Use NSCalendar:

NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
fromDate:[NSDate date]];
[components setHour: 20];
[components setMinute: 0];
[components setSecond: 0];
NSDate *myDate = [myCalendar dateFromComponents:components];
pheelicks
You can just omit the units you want to set to 0 from the components you get out of the date if you want to be more concise.
Chuck
True, but it illustrates the more general case
pheelicks
ah crazy, I went the same road but figured that there must be a "1-line-solution" to that... thank you very much!
samsam
@samsam: There used to be NSCalendarDate, which was probably more along the lines of what you were expecting. Apple apparently decided it was conceptually naughty to have all this calendar information actually embedded in the date itself, so they split the calendar out into its own class and left NSDate just to represent a raw point in time.
Chuck
@chuck: thx, i just had a look at the deprecated NSCalenderDate Class.. I understand why apple decided to "remove" it but on the other hand it woul've been much easyer for noncomplex operations... and i think these kind of operations are the ones used by the vast majority of iPhone apps..
samsam