如何在iPhone中查找当前周的最后一天?

如何在iPhone中查找当前周的最后一天?,iphone,Iphone,在我的应用程序中,我使用以下代码检索当前日期: NSDate *today1 = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"dd/MM/yyyy :EEEE"]; NSString *dateString11 = [dateFormat stringFromDate:today1]; NSLog(

在我的应用程序中,我使用以下代码检索当前日期:

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);
但使用上面的代码,我只得到了当前日期和一周的第一天,但我需要得到一周的最后一天

我的代码哪里错了,需要做什么修改才能得到最后一天/星期几?

试试这个

[components1 setDay:([components1 day]-([components1 weekday]-1) + 6)];

NSDate *endOfWeek1 = [gregorian11 dateFromComponents:components1];
它会打印出来

日期:27/04/2010:星期二 周末:2010年5月1日:周六


我写“周末”而不是第一天……

如果您有一个NSDate,您可以使用当前NSCalendar来检索该日期的NSDate组件。将NSDateComponents的工作日设置为1(一周的第一天),创建副本,并将副本的工作日设置为7(一周的最后一天)。然后使用NSCalendar将两个NSDate组件转换回各自的NSDate对象,然后可以使用NSDateFormatter创建字符串表示形式

此链接有一个关于如何获取日期工作日的示例:

vikingosegundo建议的另一种方法是同时获得开始日和结束日 . 它提供特定时间单位的开始和间隔。使用它可以很容易地在使用过的日历中找到一周的开始,并添加范围-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.
对我来说,这很有效:

计算一周中的第一天

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents     = [gregorian components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];
计算从今天开始减去的天数,以获得一周的第一天。在这种情况下,一周的第一天是星期一。这表示为首先用工作日整数减去0,然后将2加到设定日

星期日=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 *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents     = [gregorian components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
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];

希望这有帮助

[components1 setDay:([components1 day]-([components1 weekday]-1)+6];现在=[gregorian dateFromComponents:components1];[格式setDateFormat:@“dd/MM/yyyy:EEEE”];dateString=[format stringFromDate:now];NSLog(@“上周日期:%@”,日期字符串);但是上面的代码输出是==上周的日期:29/04/2010:Thursday,这很奇怪。你得到了一周的第一天-这是正确的,但是在你加上6天之后,你得到了第四天。好吧,我建议增加9而不是6——这应该行得通。对于所有阅读a.)的人来说,这是一个概念,你将它与一个日历绑定,B.)你对数字进行硬编码,不保证大小和/或偏移量。AKA,你不应该说+6,并期望它在本周末抵消。请在代码中添加一些注释,这有助于理解你的解决方案建议。检查此项以及如何给出正确答案。