Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create NSDate Object representing "today" at a certain Time

I'm either 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 today's date and a fixed 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

like image 390
samsam Avatar asked Mar 09 '10 17:03

samsam


1 Answers

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];
like image 152
pheelicks Avatar answered Oct 20 '22 04:10

pheelicks