iosobjective-cnsdatenscalendarnsdatecomponents

Get All Weeks with start date and end date of given month (NSDate Objective C)


i am trying to list all weeks of a given month like start date 6 sep 2020 end date 12 sep 2020

and if a week is ending in next month it should also display that date like start date 27 sep 2020 end date 3 oct 2020

a function which takes NSDate as input and returns array of dates (start and end date of the week) with NSArray or NSDictionary anything will work just needed start and end dates of given NSDate

i have tried DateTools, Datez and other pods but still not able to get this

    -(void) getWeeksOfTheMonthCurrent {
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    for(int currentdateindexpath=0;currentdateindexpath<=10currentdateindexpath++)
    {

        NSDateComponents* subcomponent = [calendar components:NSCalendarUnitWeekday
                                                     fromDate:[NSDate date]];
        
        [oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
        
        NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                            toDate:[NSDate date] options:0];
        [formatter setDateFormat:@"dd/MM/yy"];
        NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
        NSLog(@"weekstartdate %@",weekstartdate);

        
        NSDateComponents *components =
        [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |
                              NSCalendarUnitDay) fromDate: beginningOfWeek];
        beginningOfWeek = [calendar dateFromComponents:components];
        
        [oneDayAgoComponents setDay:7- ([subcomponent weekday])];
        
        NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                      toDate:[NSDate date] options:0];
        [formatter setDateFormat:@"dd/MM/yy"];
        NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
        NSLog(@"weekendtdate %@",weekendtdate);
    }
    
    
}

i have tried this code for getting start week date and end week date but this is not what i want i simply want a function which takes NSDate as parameter and returns a array of objects which contains start week date and end week date


Solution

  • You don't need to use 3rd party libraries to do this, there are plenty of supplied date functions for this kind of thing (see mainly the documentation for NSCalendar).

    This answer is in Swift, but I'll leave you to figure out the conversion.

    let calendar = Calendar.current
    let now = Date()
    
    let monthInterval = calendar.dateInterval(of: .month, for: now)!
    
    var weekIntervals: [DateInterval] = []
    var startDate = monthInterval.start
    
    while monthInterval.contains(startDate) {
        weekIntervals.append(calendar.dateInterval(of: .weekOfMonth, for: startDate)!)
        startDate = calendar.date(byAdding: .weekOfYear, value: 1, to: startDate)!    
    }
    
    // Not sure why this is needed, seems like the above should handle it
    let finalWeek = calendar.dateInterval(of: .weekOfMonth, for: startDate)!
    if finalWeek.start < monthInterval.end {
        weekIntervals.append(finalWeek)
    }
    

    Printing the results gives…

    let formatter = DateIntervalFormatter()
    formatter.dateStyle = .medium
    weekIntervals.forEach {
        print(formatter.string(from: $0)!)
    }
    
    
    // Jul 26, 2020, 12:00 AM – Aug 2, 2020, 12:00 AM
    // Aug 2, 2020, 12:00 AM – Aug 9, 2020, 12:00 AM
    // Aug 9, 2020, 12:00 AM – Aug 16, 2020, 12:00 AM
    // Aug 16, 2020, 12:00 AM – Aug 23, 2020, 12:00 AM
    // Aug 23, 2020, 12:00 AM – Aug 30, 2020, 12:00 AM
    // Aug 30, 2020, 12:00 AM – Sep 6, 2020, 12:00 AM