在我的应用程序中,我使用以下代码检索当前日期和日期:
NSDate *today1 = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy :EEEE"];
NSString *dateString11 = [dateFormat stringFromDate:today1];
NSLog(@"date: %@", dateString11);
//[dateFormat release];
NSCalendar *gregorian11 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components1 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];
[components1 setDay:([components1 day]-([components1 weekday]-1))];
NSDate *beginningOfWeek1 = [gregorian11 dateFromComponents:components1];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"dd/MM/yyyy :EEEE"];
NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek1];
NSLog(@"First_date: %@", dateString_first);
但是使用上面的代码我只得到当天和日期以及一周的第一天,但我需要得到一周的最后一天。
我的代码中哪里错了,需要哪些修改才能获得最后一天/一周的日期?
答案 0 :(得分:3)
对我而言,这有效:
计算一周的第一天
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
计算从今天开始减去的天数,以便获得一周的第一天。在这种情况下,一周的第一天是星期一。这表示为首先用工作日整数减去0,然后将2添加到setDay。
星期日= 1,星期一= 2,星期二= 3,星期三= 4,星期四= 5,星期五= 6,星期六= 7.通过在此整数中添加更多,您将进入下周。
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: (0 - [weekdayComponents weekday]) + 2];
[componentsToSubtract setHour: 0 - [weekdayComponents hour]];
[componentsToSubtract setMinute: 0 - [weekdayComponents minute]];
[componentsToSubtract setSecond: 0 - [weekdayComponents second]];
创建第一天的日期
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];
在第一天的日期加上6,我们可以在星期日的例子中得到最后一天。
NSDateComponents *componentsToAdd = [gregorian components:NSDayCalendarUnit fromDate:beginningOfWeek];
[componentsToAdd setDay:6];
NSDate *endOfWeek = [gregorian dateByAddingComponents:componentsToAdd toDate:beginningOfWeek options:0];
希望这有帮助
答案 1 :(得分:1)
试试这个。
[components1 setDay:([components1 day]-([components1 weekday]-1) + 6)];
NSDate *endOfWeek1 = [gregorian11 dateFromComponents:components1];
答案 2 :(得分:0)
[components1 setDay:([components1 day]+([components1 weekday]+1))];
会打印
日期:27/04/2010:星期二 周末:01/05/2010:周六
我写的是“周末”,而不是第一天......
答案 3 :(得分:0)
NSDateComponents *components2 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];
[components1 setDay:(([components2 day]-([components2 weekday]-7)))];
NSDate *now =[gregorian11 dateFromComponents:components2];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd/MM/yyyy :EEEE"];
NSString *dateString = [format stringFromDate:now];
NSLog(@" week Last_date: %@", dateString);//here it gives me thursd
答案 4 :(得分:-1)
如果您有NSDate,则可以使用当前的NSCalendar来检索该日期的NSDateComponents。将NSDateComponents的工作日设置为1(一周的第一天),创建一个副本,并将副本的工作日设置为7(一周的最后一天)。然后使用NSCalendar将NSDateComponents转换回各自的NSDate对象,之后可以使用NSDateFormatter创建字符串表示。
此链接有一个关于如何获取日期工作日的示例:
http://stackoverflow.com/questions/1057349#1057405
vikingosegundo建议的另一种方法,以获得开始和结束日期
rangeOfUnit:startDate:interval:forDate:
。它为您提供特定时间单位的开始和间隔。有了它,很容易在使用的日历中找到一周的开始,并添加范围-1以获得该周的最新秒。
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfTheWeek
interval:&interval
forDate:now];
//startOfWeek holds now the first day of the week, according to locale (monday vs. sunday)
endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
// holds 23:59:59 of last day in week.