Get the date of sunday of the current week in IOs

I have searched for the solution to find the date of the sunday of the particular week.I have found questions that have been answered but it doesnt show the correct date of sunday.

NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
comps.weekday = 1;  //Sunday
comps.weekOfMonth = 1;
NSDate *sundayDate = [calendar dateFromComponents:comps];

When i print out the date it gives this.

  • Working with NSDate components in Swift
  • How do I subtract a duration from an NSDate, but not include the weekends?
  • Differences in NSDateComponents syntax?
  • How to find what day of the week for any given date using Cocoa
  • Finding time between dates in Swift
  • How can I figure out the day difference in the following example
  • 2015-07-08 10:14:00 +0000
    

    Its the current date.What am i doing wrong?

    Solutions Collect From Internet About “Get the date of sunday of the current week in IOs”

    hope this helps you :

        NSDate *today = [NSDate date];
        NSCalendar *calenderObj = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        calenderObj.firstWeekday = 1; // Sunday = 1, 
         // NSDateComponents *components = [calenderObj components:NSWeekOfYearCalendarUnit fromDate:today];
        NSDate *objDate = nil;
     [calenderObj rangeOfUnit:NSWeekCalendarUnit startDate:&objDate interval:NULL forDate:today];
        NSLog(@" === %@",  objDate);
    

    Output :

    === 2015-07-15 10:26:00 +0000