如何获取当月特定日期的日期

时间:2017-01-06 10:22:24

标签: ios objective-c

问题是第一次约会是2016年12月31日,但它应该是1/1/2017类似于当月的最后一个日期。请解决此问题。谢谢你

    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-01-06 15:43:54.378 Ala [75024:5000910] 2017-01-06 10:13: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 个答案:

答案 0 :(得分:0)

NSDate调试打印有点误导,它会在本地时区打印与UTC相比的时间。 2016-12-31 18:30:00 UTC应该是您当地时区的2016-01-01 00:00:00。所以调试输出是正确的。

NSDate本身并不知道人类如何看待和思考日期。这只是一个时间戳。