Difference between NSDate and NSDateComponent value for the same date?

Your dates are being printed with a timezone of +0000 (UTC), while your NSCalendar instance (and therefore your NSDateComponents) is using your device's default timezone (which I would guess is UTC+2 ).

Up vote 0 down vote favorite share g+ share fb share tw.

I created a simple function to get first and last day of a week for a day in it. Looking at the NSLog output I found that different values are returned from a NSDate descriptor and component day for the same date, why? Here NSLog outputs: NSDATE: 2011-04-03 22:00:00 +0000, DAY COMPONENT: 4 NSDATE: 2011-04-09 22:00:00 +0000, DAY COMPONENT: 10 As you can see, NSDATE is 3 of April and day component is 4 for the first row, and respectively 9 and 10 for the second one.

Here the code: NSDate *date = NSDate date; //Today is April 5th 2011 NSCalendar *cal =NSCalendar allocinitWithCalendarIdentifier:NSGregorianCalendar; cal setFirstWeekday:2; //My week starts from Monday //DEFINE BEGINNING OF THE WEEK NSDate *beginningOfWeek = nil; cal rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:nil forDate:date; NSDateComponents *beginComponents = cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:beginningOfWeek; //DEFINE END OF THE WEEK, WITH 6 days OFFSET FROM BEGINNING NSDateComponents *offset = NSDateComponents allocinit; offset setDay:6; NSDate *endOfWeek = cal dateByAddingComponents:offset toDate:beginningOfWeek options:0; NSDateComponents *endComponents = cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:endOfWeek; NSLog(@"NSDATE: %@, DAY COMPONENT: %d",beginningOfWeek, beginComponents day); NSLog(@"NSDATE: %@, DAY COMPONENT: %d",endOfWeek, endComponents day); objective-c ios nsdate link|improve this question asked Apr 5 '11 at 17:06MatterGoal875420 89% accept rate.

I get current date output and I see that is forward 2 hours. At the date I launch the function my clock is at 19:16, look here the output: "TODAY 2011-04-05 17:16:20 +0000" is 17.16 and not 19.16. In my question I see that outputs are forward 2 hour too reporting 22:00 and not 24:00 (thus, right day) – MatterGoal Apr 5 '11 at 17:22.

Your dates are being printed with a timezone of +0000 (UTC), while your NSCalendar instance (and therefore your NSDateComponents) is using your device's default timezone (which I would guess is UTC+2).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions