2017-07-31 225 views

回答

0

獲得第一次約會

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

    } 
-1

你得到第一次和自己當月的最後日期。無需從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); 

這樣你得到的第一個和最後一個日期。

+0

我知道,但我使用fs calener作爲我的應用程序。當我從fscalender更改我的月份時,我想使用fs日曆得到1&31日期 –

+0

這與您從FSCalendar獲取日期或手動計算完全相同。但是從這段代碼中,您可以從fscalendar獲得的日期中獲得適當的第一天和最後一天。 –

0

您可以使用您的需求,這兩個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; 
} 

FSCalendarNSDate

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

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

    // convert back if you need to 
} 
+0

NSDate * date = ... //轉換到此處嗎?我通過哪個日期 –

+0

你是如何得到當前日期的? – nayem

+0

本月自動加載 –

0

FSCalendar屬性currentPage返回起始日期當前顯示的頁面 - 這是每月或每週的第一天,具體取決於您是顯示一個月還是一週。

然後使用NSCalendar方法查找以currentPage開頭的月份或周的最後一天。有很多方法可以做到這一點;例如添加一個NSDateComponents值或使用「下一個」方法之一,如nextDateAfterDate:matchingUnit:value:options:

HTH

0
-(void)calendarCurrentPageDidChange:(FSCalendar *)calendar{ 
    [self.calendar selectDate:[self.calendar.currentPage fs_firstDayOfMonth]]; 
    [self.calendar selectDate:[self.calendar.currentPage fs_lastDayOfMonth]]; 
} 
相關問題