Note that there are some explanatory texts on larger screens.

plurals
  1. POIOS 4.1 NSDateComponents giving differing date then later versions of IOS
    primarykey
    data
    text
    <p>I have created an NSDate category that would give me a "last sunday" date.</p> <pre><code>+(NSDate *)sunday{ // I need 2 dates yesterday and today NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now = [NSDate date]; NSLog(@"Current date: %@", now); NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now]; [components setWeekday:1]; //Monday [components setHour:0]; //8a.m. [components setMinute:0]; [components setSecond:0]; NSLog(@"Sunday: %@", [gregorian dateFromComponents:components]); return [gregorian dateFromComponents:components]; } </code></pre> <p>This give me a couple different dates:</p> <p>IOS 4.2 </p> <pre><code>Sunday: 0001-08-28 05:50:36 +0000 </code></pre> <p>IOS 4.1 </p> <pre><code>Sunday: 0001-09-04 05:50:36 GMT </code></pre> <p><strong>Notice</strong> That IOS 4.1 give me a future date, whereas IOS 4.2 gives me a previous date, which is what I want.</p> <p>I understand that the year is 0001 is shown, but the year is not used. </p> <p>I have not yet found where others are having this same issue, so perhaps I'm doing something wrong. But, I'm not sure what that is. Has anyone seen this before? </p> <p><strong>EDIT</strong> Here is my working code:</p> <pre><code>+(NSDate *)sunday{ NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now = [NSDate date]; NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now]; [components setWeekday:[gregorian firstWeekday]]; //Sunday [components setHour:0]; [components setMinute:0]; [components setSecond:0]; return [gregorian dateFromComponents:components]; } </code></pre> <p>Thanks!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload