I have this piece of code. I get the current date and time and keep the day/month/year part, changing the time.
The first thing I notice is that the current date and time is wrong. One hour before the correct time. Then, I change the time to my own time and it is wrong again.
This is the code:
NSDate *today = [NSDate date];
// now is june 28, 17:39 pm, printing this to console gives me
// june 28, 16:39 pm... one hour before... this is the first problem
// then I change the time...
// get the current day, month and year
NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *dayX = [gregorian components: NSDayCalendarUnit fromDate: data];
NSDateComponents *monthX = [gregorian components: NSMonthCalendarUnit fromDate: data];
NSDateComponents *yearX = [gregorian components: NSYearCalendarUnit fromDate: data];
NSInteger day = [dayX day];
NSInteger month = [monthX month];
NSInteger year = [yearX year];
// this gives me the correct day, month and year
// now, create a new date using the current day, month and year but with new hour and minute...
NSDateComponents * dateComponents = [[[NSDateComponents alloc] init] autorelease];
[dateComponents setHour:17];
[dateComponents setMinute:32];
[dateComponents setDay:day];
[dateComponents setMonth:month];
[dateComponents setYear:year];
// the new date is: 16:32?????
// how can that be if I just set it 17:32??????????
any clues?
thanks