2017-01-06 83 views
1

問題是第一次約會是31/12/2016,但它應該是2017年1月1日與當前month.Please的最後一天類似的解決這個。謝謝如何獲得當月的某一天的日期

NSInteger wantedWeekDay = 7; //for saturday 

    //set current date here 
    NSDate *currentDate = [NSDate date]; 
    NSLog(@"%@", currentDate); 
    //get calender 
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    // Start out by getting just the year, month and day components of the current date. 
    NSDateComponents *components = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSCalendarUnitWeekday fromDate:currentDate]; 
    // Change the Day component to 1 (for the first day of the month), and zero out the time components. 
    [components setDay:1]; 

    [components setHour:0]; 
    [components setMinute:0]; 
    [components setSecond:0]; 

    //get first day of current month 
    NSDate *firstDateOfCurMonth = [gregorianCalendar dateFromComponents:components]; 
    NSLog(@"first date is %@", firstDateOfCurMonth); 
    //create new component to get weekday of first date 
    NSDateComponents *newcomponents = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSCalendarUnitWeekday fromDate:firstDateOfCurMonth]; 
    NSInteger firstDateWeekDay = newcomponents.weekday; 
    NSLog(@"weekday : %li",(long)firstDateWeekDay); 



    //get last month date 
    NSInteger curMonth = newcomponents.month; 

    NSLog(@"current month is %ld",(long)curMonth); 

    [newcomponents setMonth:curMonth+1]; 

    NSDate * templastDateOfCurMonth = [[gregorianCalendar dateFromComponents:newcomponents] dateByAddingTimeInterval: -1]; // One second before the start of next month 

    NSDateComponents *lastcomponents = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSCalendarUnitWeekday fromDate:templastDateOfCurMonth]; 

    [lastcomponents setHour:0]; 
    [lastcomponents setMinute:0]; 
    [lastcomponents setSecond:0]; 

    NSDate *lastDateOfCurMonth = [gregorianCalendar dateFromComponents:lastcomponents]; 

    NSLog(@"last date is %@",lastDateOfCurMonth); 

    NSMutableArray *mutArrDates = [NSMutableArray array]; 

    NSDateComponents *dayDifference = [NSDateComponents new]; 
    [dayDifference setCalendar:gregorianCalendar]; 

    //get wanted weekday date 
    NSDate *firstWeekDateOfCurMonth = nil; 
    if (wantedWeekDay == firstDateWeekDay) { 
     firstWeekDateOfCurMonth = firstDateOfCurMonth; 
    } 
    else 
    { 
     NSInteger day = wantedWeekDay; 
     if (day < 0) 
      day += 7; 
     ++day; 
     [components setDay:day]; 

     firstWeekDateOfCurMonth = [gregorianCalendar dateFromComponents:components]; 
     NSLog(@"firstWeekDateOfCurMonth %@",firstWeekDateOfCurMonth); 
    } 
    NSLog(@"%@",firstWeekDateOfCurMonth); 

    NSUInteger weekOffset = 0; 
    NSDate *nextDate = firstWeekDateOfCurMonth; 

    do { 
     [mutArrDates addObject:nextDate]; 
     [dayDifference setWeekOfYear:++weekOffset]; 
     NSDate *date = [gregorianCalendar dateByAddingComponents:dayDifference toDate:firstWeekDateOfCurMonth options:0]; 
     nextDate = date; 
    } while([nextDate compare:lastDateOfCurMonth] == NSOrderedAscending || [nextDate compare:lastDateOfCurMonth] == NSOrderedSame); 
    NSLog(@"%@",mutArrDates); 

輸出是 2017年1月6日15:43:54.378丙氨酸[75024:5000910] 2017年1月6日10時十三分50秒0000

2017-01-06 15:44:10.592 Ala[75024:5000910] first date is 2016-12-31 
18:30:00 +0000 

2017-01-06 15:44:54.869 Ala[75024:5000910] current month is 1 

2017-01-06 15:45:13.684 Ala[75024:5000910] last date is 2017-01-30 18:30:00 +0000 

2017-01-06 15:45:27.991 Ala[75024:5000910] firstWeekDateOfCurMonth 2017-01-07 18:30:00 +0000 
2017-01-06 15:45:27.991 Ala[75024:5000910] 2017-01-07 18:30:00 +0000 
2017-01-06 15:45:50.863 Ala[75024:5000910] (
    "2017-01-07 18:30:00 +0000", 
    "2017-01-14 18:30:00 +0000", 
    "2017-01-21 18:30:00 +0000", 
    "2017-01-28 18:30:00 +0000" 
) 
+1

你需要的不是'NSDate'對象,你需要打印的是'NSDate'轉換爲日期格式的字符串,那麼它可能會顯示你想要的正確日期 – Tj3n

+0

請詳細說明你的問題正是你想要的? – seggy

+0

不清楚你想問什麼。請清除你的問題。 – CodeChanger

回答

0

的NSDate調試打印有點誤導,它與UTC相比,可在當地時區打印時間。 2016-12-31 18:30:00 UTC應該完全在您當地的時區2016-01-01 00:00:00。所以調試輸出是正確的。

NSDate本身不知道人類如何理解和思考日期。這只是一個時間戳。

相關問題