参考预先回答的问题:Getting first and last days of current week
以上链接有两个答案。其中一个是理论上的,另一个是PyObjC(Python-Objective C桥接语言),快速谷歌搜索确认PyObjC不适用于iPhone SDK。
关于这个问题,如何将PyObjC代码翻译成与iPhone SDK兼容。
目标:假设今天(星期二)是19日,太阳。是第17次(周开始)和周六。 23日是周末。我希望得到像19/01 - 23/01这样的字符串[即周开始(炒作)周末]
答案 0 :(得分:7)
如果您有NSDate
,则可以使用当前NSCalendar
来检索该日期的NSDateComponents
。将NSDateComponents
的{{1}}设置为1(一周的第一天),创建副本,并将副本的weekday
设置为7(一周的最后一天)。然后使用weekday
将NSCalendar
转换回各自的NSDateComponents
对象,之后您可以使用NSDate
创建字符串表示形式。
此链接有一个关于如何获取日期工作日的示例:https://stackoverflow.com/questions/1057349#1057405
答案 1 :(得分:3)
以下是一些代码,它还会检查上周开始的一个边缘情况:
// Finds the date for the first day of the week
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
// Edge case where beginning of week starts in the prior month
NSDateComponents *edgeCase = [[NSDateComponents alloc] init];
[edgeCase setMonth:2];
[edgeCase setDay:1];
[edgeCase setYear:2013];
NSDate *edgeCaseDate = [calendar dateFromComponents:edgeCase];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:edgeCaseDate];
[components setWeekday:1]; // 1 == Sunday, 7 == Saturday
[components setWeek:[components week]];
NSLog(@"Edge case date is %@ and beginning of that week is %@", edgeCaseDate , [calendar dateFromComponents:components]);
// Find Sunday for the given date
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
[components setWeekday:1]; // 1 == Sunday, 7 == Saturday
[components setWeek:[components week]];
NSLog(@"Original date is %@ and beginning of week is %@", givenDate , [calendar dateFromComponents:components]);
return [calendar dateFromComponents:components];
}
编辑:上面的相关代码
- (NSDate *)firstDayOfWeekFrom:(NSDate *)givenDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
[components setWeekday:1]; // 1 == Sunday, 7 == Saturday
[components setWeekOfYear:[components weekOfYear]];
return [calendar dateFromComponents:components];
}
答案 2 :(得分:0)
这是我几天前找到的,终于找到了。 rangeOfUnit:startDate:interval:forDate:
中有一种方法NSDate
,并以简单的方式执行此操作。你可以在:
Current Week Start and End Date
答案 3 :(得分:0)
NSDate *weekDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:weekDate];
int ff = currentComps.weekOfYear;
NSLog(@"1 %d", ff);
[currentComps setWeekday:1]; // 1: sunday
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7]; // 7: saturday
NSDate *lastDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"dd EEEE";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:lastDayOfTheWeek];
NSLog(@"first - %@ \nlast - %@", firstStr, secondStr);