如何在ios sdk中获取当月所有星期一的所有日期?
例如,我希望所有星期一的日期发生在2015年1月
下面的代码给出了nsdate的月,日和年。但是现在我想要那个月的工作日(星期一)的nsdate。
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; // Get necessary date components
[components month]; //gives you month
[components day]; //gives you day
[components year]; // gives you year
答案 0 :(得分:3)
//Set Wantedday here with sun=1 ..... sat=7;
NSInteger wantedWeekDay = 2; //for monday
//set current date here
NSDate *currentDate = [NSDate date];
//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];
//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;
[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(@"%@",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 - firstDateWeekDay;
if (day < 0)
day += 7;
++day;
[components setDay:day];
firstWeekDateOfCurMonth = [gregorianCalendar dateFromComponents:components];
}
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);
答案 1 :(得分:2)
基本步骤
这是一个如何做到这一点的例子
- (NSArray *) datesForWeekday:(NSInteger)weekday forMonth:(NSInteger)month andYear:(NSInteger)year
{
unsigned int units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
| NSCalendarUnitWeekday;
// Step 1. create an NSDate for the first of the month
NSDate *date = [self dateWithMonth:month day:1 andYear:year];
// Step 2. determine the day of the week (1=Sunday, 2=Monday, ..., 7=Saturday
NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:date];
NSInteger firstDayOfMonth = [comps weekday];
// Step 3. offset so the day is the day of the week you are interested in
NSInteger day = weekday - firstDayOfMonth;
if (day < 0)
day += 7;
++day;
NSMutableArray *array = [NSMutableArray new];
NSUInteger numberOfDaysInMonth = [self numberOfDaysWithDate:date];
// Step 4. add 7 to the day until we reach the end of the month
do {
// Add NSDate object to array
[array addObject:[self dateWithMonth:month day:day andYear:year]];
// or you can optionally add just the day to the array
// [array addObject:@(day)];
day += 7;
} while (day <= numberOfDaysInMonth);
return array;
}
// Returns an NSDate object for the specified month, day, and year
- (NSDate *) dateWithMonth:(NSInteger)month day:(NSInteger)day andYear:(NSInteger)year
{
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:0];
[dateComps setMinute:0];
return [[NSCalendar currentCalendar] dateFromComponents:dateComps];
}
// Determines the number of days in the month for specified date
- (NSUInteger) numberOfDaysWithDate:(NSDate *)date
{
NSRange days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitMonth
forDate:date];
return days.length;
}
以下是2015年1月查找所有星期一的示例
NSArray *dates = [self datesForWeekday:2 forMonth:1 andYear:2015];
或2018年12月的所有星期三
NSArray *dates = [self datesForWeekday:4 forMonth:12 andYear:2018];
或当月的所有星期一
NSDate *date = [NSDate date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:date];
NSArray *dates = [self datesForWeekday:2 forMonth:[comps month] andYear:[comps year]];
答案 2 :(得分:1)
使用(NS)Calendar
和NSCalendarUnitWeekdayOrdinal
的{{1}}组件的简单解决方案。
获取当前日期(NS)DateComponents
,year
和month
的组件。然后循环获取所有顺序工作日,直到月份部分超过当前月份为止
Objective-C:
weekdayOrdinal
快捷键:
- (NSArray<NSDate *> *)datesOfCurrentMonthWith:(NSInteger)weekday {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekdayOrdinal fromDate:[NSDate date]];
components.weekday = 1;
NSMutableArray<NSDate *> *result = [NSMutableArray array];
for (NSInteger ordinal = 1; ordinal < 6; ordinal++) { // maximum 5 occurrences
components.weekdayOrdinal = ordinal;
NSDate *date = [calendar dateFromComponents:components];
if ([calendar component:NSCalendarUnitMonth fromDate:date] != components.month) { break; }
[result addObject:[calendar dateFromComponents: components]];
}
return [result copy];
}
答案 3 :(得分:0)
只是通过你星期一得到的所有月份日期!我已经创建了一个例子,它在当前日期工作正常!
在viewDidLoad
中NSDate *dt = [NSDate date];
NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components: NSCalendarUnitEra |NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay fromDate:dt];
NSRange days = [gregorian rangeOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitMonth
forDate:dt];
for (int i=1; i<days.length+1; i++)
{
comp.day = i;
if([self isMonday:[gregorian dateFromComponents:comp]])
{
NSLog(@"Monday %@",[gregorian dateFromComponents:comp]);
}
}
-(BOOL)isTodayMonday:(NSDate*)dt
{
BOOL isMonday;
NSDateFormatter *datef = [[NSDateFormatter alloc]init];
datef.dateFormat = @"EEEE";
NSString *strDate = [datef stringFromDate:dt];
if([strDate isEqualToString:@"Monday"])
{
NSLog(@"monday date %@",dt);
isMonday = YES;
}
else
{
isMonday = NO;
}
return isMonday;
}
您可以参考here