我正在制定学生时间表,但我遇到了问题。我需要有本周和下周的一系列日子。因此,如果今天是星期二11.11,我需要以某种方式获得星期一10.11星期二11.11星期三12.11星期四13.11星期五星期四14.11,以及下周的同一天日期。这个我需要在我的程序中执行这样的功能,当用户可以选择一周中的任何一天并在这一天设置他的日程安排。我在堆栈溢出时到处查看,但所有解决方案都不准确。你能帮助我吗?这是我的一些尝试。
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSDate *beginningOfNextWeek;
NSTimeInterval durationOfWeek;
[calendar setFirstWeekday:3];
[calendar rangeOfUnit:NSCalendarUnitWeekOfMonth
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
beginningOfNextWeek = [beginningOfThisWeek dateByAddingTimeInterval:durationOfWeek];
NSDate *date = beginningOfThisWeek;
NSMutableArray *dateArray = [@[] mutableCopy];
while (![beginningOfNextWeek compare:date] == NSOrderedSame) {
[dateArray addObject:date];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:date];
comps.day +=1;
date = [calendar dateFromComponents:comps];
}
NSLog(@"%@",dateArray);
NSLog(@"%@",now);
但在日志中我会得到类似的内容:
2014-11-11 18:37:29.479 StudentSchedule[3964:436389] ( "2014-11-10 22:00:00 +0000", "2014-11-11 22:00:00 +0000", "2014-11-12 22:00:00 +0000", "2014-11-13 22:00:00 +0000", "2014-11-14 22:00:00 +0000", "2014-11-15 22:00:00 +0000", "2014-11-16 22:00:00 +0000"
我有约会,但是7,我没有工作日的名字,这非常重要。我知道我的问题很简单,但我厌倦了寻找正确的答案。
答案 0 :(得分:0)
好的,所以说你在某一天打开应用程序。
您希望该周的所有工作日(周一至周五)以及下周的所有工作日。
右...
- (void)getDays
{
NSArray *days = @[];
NSDate *currentDate = [NSDate date];
// add this week's work days to array
days = [days arrayByAddingObjectsFromArray:[self workDaysOfWeekFromDate:currentDate]];
NSDate *nextWeek = [self dateWeekAfterDate:currentDate];
// add next week's work days to array
days = [days arrayByAddingObjectsFromArray:[self workDaysOfWeekFromDate:nextWeek]];
}
- (NSDate *)dateWeekAfterDate:(NSDate *)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [NSDateComponents new];
components.day = 7;
return [calendar dateByAddingComponents:components toDate:date options:0];
}
- (NSInteger)dayOfWeekFromDate:(NSDate *)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarWeekdayUnit fromDate:date];
return components.weekday;
}
- (NSDate *)dayWithIndex:(NSInteger)dayIndex fromWeekOfDate:(NSDate *)date
{
NSInteger dayOfWeekFromDate = [self dayOfWeekFromDate:date];
NSInteger difference = dayIndex - dayOfWeekFromDate;
NSDateComponents *components = [NSDateComponents new];
components.day = difference;
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:components toDate:date options:0];
}
- (NSArray *)workDaysOfWeekFromDate:(NSDate *)date
{
// Sunday = 1
// ...
// Saturday = 7
return @[
[self dayWithIndex:2 fromWeekOfDate:date], // Monday
[self dayWithIndex:3 fromWeekOfDate:date], // Tuesday
[self dayWithIndex:4 fromWeekOfDate:date], // Wednesday
[self dayWithIndex:5 fromWeekOfDate:date], // Thursday
[self dayWithIndex:6 fromWeekOfDate:date] // Friday
];
}
这里有几个功能。一切都在getDays
函数中完成并完成。完成后,数组days
将在本周和下周的周一至周五包含NSDate对象。
当然,您可以将currentDate
更改为您想要的任何日期。它始终需要currentDate
的一周和currentDate
之后的一周。
这将始终有效,任何日历和闰年等......
要获取日期的名称,您可以执行此操作...
NSDate *date = [NSDate date];
NSDateFormatter *df = [NSDateFormatter new];
df.dateFormat = @"EEEE";
NSString *dayName = [df stringFromDate:date];
格式EEEE
表示日期名称。所以今天dayName
字符串是Tuesday
。
EEEE = Tuesday
EEE = Tue
你当然可以将它合并为一个功能较少的功能。但我更喜欢可读的代码。