我想从今天和每周发布一个NSDate
到今天。
E.g。周= 2
和日= 4
,从现在起thursday
周内为two
(假设星期一开始)。
我使用NSCalendar
和NSDateComponents
尝试了很多不同的东西,但没有任何结果。
搜索了一下,但没有找到关于我的问题的任何其他话题。
我尝试的最后一件事是:
- (NSDate *)getDateForIndexPath:(NSIndexPath *)indexPath
{
int week = ((indexPath.row / 7) % 5);
int day = (indexPath.row % 7);
NSDate *referenceDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"]];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit | NSWeekCalendarUnit |NSWeekdayCalendarUnit) fromDate:referenceDate];
[dateComponents setWeekday:day];
[dateComponents setWeek:dateComponents.week+(week-1)];
[dateComponents setWeek:dateComponents.week];
NSDate *followingDate = [calendar dateFromComponents:dateComponents];
NSLog(@"Week: %d - Day: %d", week, day);
NSLog(@"followingDate: %@", followingDate);
return followingDate;
}
这给了我一遍又一遍的日期,除了它将第0天视为星期日:
2014-04-08 00:02:33.706 TVSports[79878:60b] Week: 1 - Day: 0
2014-04-08 00:02:33.707 TVSports[79878:60b] followingDate: 2014-04-11 22:00:00 +0000
2014-04-08 00:02:33.708 TVSports[79878:60b] Week: 1 - Day: 1
2014-04-08 00:02:33.708 TVSports[79878:60b] followingDate: 2014-04-05 22:00:00 +0000
2014-04-08 00:02:33.709 TVSports[79878:60b] Week: 1 - Day: 2
2014-04-08 00:02:33.709 TVSports[79878:60b] followingDate: 2014-04-06 22:00:00 +0000
2014-04-08 00:02:33.710 TVSports[79878:60b] Week: 1 - Day: 3
2014-04-08 00:02:33.710 TVSports[79878:60b] followingDate: 2014-04-07 22:00:00 +0000
答案 0 :(得分:2)
此:
NSDate *referenceDate = [NSDate date];
...
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit | NSWeekCalendarUnit |NSWeekdayCalendarUnit) fromDate:referenceDate];
返回今天正确填充的日期组件,因为您已要求今天的日期,然后要求日历将其分解为年,月,周和工作日。
之后,这:
[dateComponents setWeekday:day];
[dateComponents setWeek:dateComponents.week+(week-1)];
[dateComponents setWeek:dateComponents.week];
设定日期。然后它读取当前的设定周并向其添加week-1
(因此第1周将是本周,第2周将是下周等)。然后它再次冗余地读取一周并重新设置它。
您的输出显示代码正常工作。 Apple是一家美国公司,所以工作日0对他们来说总是星期天。但是他们知道星期天不是大多数地方的一周的第一天所以你的代码要求“星期日(第0天),本周” - 而不是“本周日如果我们假装星期日星期一开始” 。它说周日是第11个。允许时区差异,这是正确的。
请注意NSDate
s 没有时区。 NSDate
是特定时刻的不透明记录。如何沟通那一刻并不重要。他们为了能够说些什么而登录GMT。
然后你要求周一,周二和周三,它将返回第5,第6和第7。一切都正确。
旁注:只需使用:
NSCalendar *calendar = [NSCalendar currentCalendar];
使用当前系统日历。
答案 1 :(得分:1)
你可以这样做,以便在两周内获得星期四:
typedef NS_ENUM(NSUInteger, WeekDay){
WeekDaySunday = 1,
WeekDayModay,
WeekDayTuesday,
WeekDayWensday,
WeekDayThursday,
WeekDayFriday,
WeekDaySaturday
};
NSDate *now = [NSDate date];
NSDate *beginOfThisWeek;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setFirstWeekday:WeekDayModay];
[cal rangeOfUnit:NSWeekCalendarUnit // get the beginning of this week
startDate:&beginOfThisWeek // save the beginning to this variable
interval:NULL // we do not care for the weeks length
forDate:now];
NSDateComponents *twoWeeks = [[NSDateComponents alloc] init];
twoWeeks.week = 2;
twoWeeks.day = WeekDayThursday - WeekDaySunday -1 ; // we need the difference between thursday and the beginning of the week
NSDate *thurdayInTwoWeeks = [cal dateByAddingComponents:twoWeeks
toDate:beginOfThisWeek
options:0];