[英]How to get dates of specific day of current month
問題是第一次約會是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"
)
NSDate調試打印有點誤導,它會在本地時區打印與UTC相比的時間。 2016-12-31 18:30:00 UTC應該是您當地時區的2016-01-01 00:00:00。 所以調試輸出是正確的。
NSDate本身並不知道人類如何看待和思考日期。 這只是一個時間戳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.