如何獲取月曆開始和結束日期?如何獲取ios中fscalender的第一個日期和最後一個日期?
從截圖中,我需要在fscalender中獲得2017年8月1日和2017年3月31日?當我滾動
然後調用此方法,但每次得到的當前日期
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar;
{
}
的感謝!
如何獲取月曆開始和結束日期?如何獲取ios中fscalender的第一個日期和最後一個日期?
從截圖中,我需要在fscalender中獲得2017年8月1日和2017年3月31日?當我滾動
然後調用此方法,但每次得到的當前日期
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar;
{
}
的感謝!
獲得第一次約會
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar;
{
NSLog(@"did change to page %@",[self.dateFormatter stringFromDate:calendar.currentPage]);
}
你得到第一次和自己當月的最後日期。無需從FSCalendar獲取它。爲此請參閱下面的代碼。
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);
這樣你得到的第一個和最後一個日期。
我知道,但我使用fs calener作爲我的應用程序。當我從fscalender更改我的月份時,我想使用fs日曆得到1&31日期 –
這與您從FSCalendar獲取日期或手動計算完全相同。但是從這段代碼中,您可以從fscalendar獲得的日期中獲得適當的第一天和最後一天。 –
您可以使用您的需求,這兩個NSDate
的分類方法:您需要的日期從轉換
- (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;
}
您FSCalendar
到NSDate
:
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar {
NSDate *date = ... //Conversion goes here
NSDate *startDateOfMonth = [date startDateOfMonth];
NSDate *endDateOfMonth = [date endDateOfMonth];
// convert back if you need to
}
的FSCalendar
屬性currentPage
返回起始日期當前顯示的頁面 - 這是每月或每週的第一天,具體取決於您是顯示一個月還是一週。
然後使用NSCalendar
方法查找以currentPage
開頭的月份或周的最後一天。有很多方法可以做到這一點;例如添加一個NSDateComponents
值或使用「下一個」方法之一,如nextDateAfterDate:matchingUnit:value:options:
。
HTH
-(void)calendarCurrentPageDidChange:(FSCalendar *)calendar{
[self.calendar selectDate:[self.calendar.currentPage fs_firstDayOfMonth]];
[self.calendar selectDate:[self.calendar.currentPage fs_lastDayOfMonth]];
}
得到答案...................... –