Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • iOS-Get Number of Days According to the Selected Month in Month Picker

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 829
    Comment on it

    1. Suppose you want to select or get number of days as per its month.
    2. First, use a picker & convert it into month picker you can use many third-party pods or simply use their .h or .m files also.
    3. Once ur picker is ready to go select any month & what you have to do is get the no of days of that particular month.
    4. Take two integers as yearVal & monthVal.
    5. And an NSdate variable as     NSDate *mainDate;
    5. Maintain an array for storing number of days.
    5. Insert Values in year & month values by using components of calendar as-
            monthVal = components.month;
            yearVal = components.year;
    6. Once you have these two values pass them into this function-

    -(void)getNumOfDays:(int)selectedMonth selectedYear:(int)selectedYear{
        
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSDateComponents *components = [[NSDateComponents alloc] init];
                // Set your year and month here
            [components setYear:selectedMonth];
            [components setMonth:selectedYear];
            // NSDate *date = [calendar dateFromComponents:components];
            NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:mainDate];
            if (dataSourceArray.count>1) {
            [dataSourceArray removeAllObjects];
            }
            NSLog(@"%d", (int)range.length);
            for (int i=1; i<range.length+1; i++) {
            [dataSourceArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }

     

     

    7. DataSource here is the array in which all get the number of days stored once you pass the month from your month picker for example if you have selected month as February then when you will call this function you will get the number of days i.e 28 in the Datasource array.

    8. You can then load data i.e number of days in picker via this Datasource array.

    Thanks for reading.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: