0

How can I get month calendar start and end date?

From the screenshot, I need to get 1 aug 2017 and 31 aug 2017 in fscalender?

when i scroll then this method call but everytime get current date

- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar;
{

}

thanks!

enter image description here

Maulik shah
  • 1,664
  • 1
  • 19
  • 45

5 Answers5

1

The FSCalendar property currentPage returns the start date of currently showing page - this is the first day of the month or week depending on whether you are showing a month or week.

Then use NSCalendar methods to find the last day of the month or week that starts with currentPage. There are a number of ways to do this; e.g. add an NSDateComponents value or use one of the "next" methods such as nextDateAfterDate:matchingUnit:value:options:.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

You can use these two NSDate category methods for your need:

- (NSDate *)startDateOfMonth {
    NSCalendar *current = [NSCalendar currentCalendar];
    NSDate *startOfDay = [current startOfDayForDate:self];
    NSDateComponents *components = [current components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:startOfDay];
    NSDate *startOfMonth = [current dateFromComponents:components];
    return startOfMonth;
}

- (NSDate *)endDateOfMonth {
    NSCalendar *current = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.month = 1;
    components.day = -1;
    NSDate *endOfMonth = [current dateByAddingComponents:components toDate:[self startDateOfMonth] options:NSCalendarSearchBackwards];
    return endOfMonth;
}

You need to convert the date from your FSCalendar to NSDate:

- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar {
    NSDate *date = ... //Conversion goes here

    NSDate *startDateOfMonth = [date startDateOfMonth];
    NSDate *endDateOfMonth = [date endDateOfMonth];

    // convert back if you need to
}
nayem
  • 7,285
  • 1
  • 33
  • 51
0

Get First date

 - (void)calendarCurrentPageDidChange:(FSCalendar *)calendar;
    {
        NSLog(@"did change to page %@",[self.dateFormatter stringFromDate:calendar.currentPage]);

    }
Maulik shah
  • 1,664
  • 1
  • 19
  • 45
  • Actually, I want to take the first and last date of the month. And I got the first date but I am stuck on the last date so if you have found any solution for the last date of the month then can you please help me in that. – Rushabh Shah Nov 20 '20 at 10:24
0
-(void)calendarCurrentPageDidChange:(FSCalendar *)calendar{
    [self.calendar selectDate:[self.calendar.currentPage fs_firstDayOfMonth]];
    [self.calendar selectDate:[self.calendar.currentPage fs_lastDayOfMonth]];
}
Neethu M
  • 644
  • 6
  • 11
-1

You get First and Last date of Current Month by yourself. No need to get it from FSCalendar. For this see below code.

NDDate *currentDate = //Pass date which you get
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:currentDate];
[comp setMonth:[comp month]];
[comp setDay:1];
NSDate *firstDayDate = [gregorian dateFromComponents:comp];
[comp setMonth:[comp month]+1];
[comp setDay:0];
NSDate *lastDayDate = [gregorian dateFromComponents:comp];

NSLog(@"First : %@, Last : %@",firstDayDate,lastDayDate);

This way you are getting First and Last Date.

Harshal Shah
  • 427
  • 3
  • 5
  • I know that but i use fs calener for my app.When i change my month from fscalender then i want to get 1 & 31 date using fs calender – Maulik shah Jul 31 '17 at 07:52
  • This is quite same that you get Date from FSCalendar or manually calculate. But from this code you get proper first and last day of month from date that you are getting from fscalendar. – Harshal Shah Jul 31 '17 at 09:07