Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当系统日历在周日时,NSCalendar认为是';这是新的一周_Ios_Object_Nsdate_Nscalendar - Fatal编程技术网

Ios 当系统日历在周日时,NSCalendar认为是';这是新的一周

Ios 当系统日历在周日时,NSCalendar认为是';这是新的一周,ios,object,nsdate,nscalendar,Ios,Object,Nsdate,Nscalendar,当我的系统日历设置为星期日时,下面的代码认为这是新的一周,并显示“下一周”的所有日期。 例如 本周是第39周。 9月28日星期日,我的代码认为这是新的一周,并显示下周(第40周)的所有日期,而不是我希望的第39周。 (我住的地方一周的时间是从周一到周日,而不是从周日到周六) 我的日志显示: 2014-09-21 22:40:36.040 NSCalendar项目[38667:90b] 2014-09-22 2014-09-23 2014-09-24 2014-09-25 2014-09-

当我的系统日历设置为星期日时,下面的代码认为这是新的一周,并显示“下一周”的所有日期。 例如 本周是第39周。 9月28日星期日,我的代码认为这是新的一周,并显示下周(第40周)的所有日期,而不是我希望的第39周。 (我住的地方一周的时间是从周一到周日,而不是从周日到周六)

我的日志显示: 2014-09-21 22:40:36.040 NSCalendar项目[38667:90b] 2014-09-22 2014-09-23 2014-09-24 2014-09-25 2014-09-26 2014-09-27 2014-09-28


如您所见,我运行此功能的日期是9月21日星期日,它显示下周的所有日期(如果将日期设置为9月28日,则相同)

调用[myCalendar setFirstWeekday:2]仅将第一个工作日设置为周一。然而,星期天仍然是“1”。因此,当currentDate是星期日时,“2”表示该星期日之后的星期一,代码以下周的日期结束

你真正想要的是得到一周的第一天,其中“currentDate”是其中的一部分。这段代码应该可以工作,但我不知道它是否是最优雅的解决方案

NSCalendar *myCalendar = [NSCalendar currentCalendar];

    //create a 'sunday' date object for demonstration purposes
    NSDateComponents *sundayComponents = [[NSDateComponents alloc]init];
    sundayComponents.day = 28;
    sundayComponents.month = 9;
    sundayComponents.year = 2014;
    NSDate *sunday = [myCalendar dateFromComponents:sundayComponents];

    [myCalendar setLocale:[NSLocale systemLocale]];

    NSDateComponents *currentComps = [myCalendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSCalendarUnitWeekOfYear|NSWeekdayCalendarUnit fromDate:sunday];
    int thisWeeksNumber = currentComps.weekOfYear;
    NSLog(@"1  %d", thisWeeksNumber);

    [myCalendar setFirstWeekday:2];
    NSDate *beginningOfWeek = nil;
    [myCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&beginningOfWeek
                   interval:NULL forDate: sunday];


    NSDateComponents *components = [[NSDateComponents alloc] init];

    NSDate *firstDayOfTheWeek = beginningOfWeek;
    components.day = 1;
    NSDate *secondDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 2;
    NSDate *thirdDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 3;
    NSDate *fourthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 4;
    NSDate *fifthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 5;
    NSDate *sixthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 6;
    NSDate *seventhDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];


    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    myDateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
    NSString *secondStr = [myDateFormatter stringFromDate:secondDayOfTheWeek];
    NSString *thirdStr = [myDateFormatter stringFromDate:thirdDayOfTheWeek];
    NSString *fourthStr = [myDateFormatter stringFromDate:fourthDayOfTheWeek];
    NSString *fifthStr = [myDateFormatter stringFromDate:fifthDayOfTheWeek];
    NSString *sixthStr = [myDateFormatter stringFromDate:sixthDayOfTheWeek];
    NSString *seventhStr = [myDateFormatter stringFromDate:seventhDayOfTheWeek];

    NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@ \n %@ \n %@", firstStr, secondStr, thirdStr, fourthStr, fifthStr, sixthStr, seventhStr);

    //log first day of week
    NSString *beginningWeek = [myDateFormatter stringFromDate:beginningOfWeek];
    NSLog(@"beginning of week is %@", beginningWeek);

谢谢你的回答!我现在已经解决了这个问题!这只在2014年有效。不客气,很高兴能帮上忙。在什么意义上,它只在2014年起作用?当我输入(例如)2015年1月4日星期日时,我将得到2014年12月22日星期一到2015年1月4日星期日。这不是正确的行为吗?
NSCalendar *myCalendar = [NSCalendar currentCalendar];

    //create a 'sunday' date object for demonstration purposes
    NSDateComponents *sundayComponents = [[NSDateComponents alloc]init];
    sundayComponents.day = 28;
    sundayComponents.month = 9;
    sundayComponents.year = 2014;
    NSDate *sunday = [myCalendar dateFromComponents:sundayComponents];

    [myCalendar setLocale:[NSLocale systemLocale]];

    NSDateComponents *currentComps = [myCalendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSCalendarUnitWeekOfYear|NSWeekdayCalendarUnit fromDate:sunday];
    int thisWeeksNumber = currentComps.weekOfYear;
    NSLog(@"1  %d", thisWeeksNumber);

    [myCalendar setFirstWeekday:2];
    NSDate *beginningOfWeek = nil;
    [myCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&beginningOfWeek
                   interval:NULL forDate: sunday];


    NSDateComponents *components = [[NSDateComponents alloc] init];

    NSDate *firstDayOfTheWeek = beginningOfWeek;
    components.day = 1;
    NSDate *secondDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 2;
    NSDate *thirdDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 3;
    NSDate *fourthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 4;
    NSDate *fifthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 5;
    NSDate *sixthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 6;
    NSDate *seventhDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];


    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    myDateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
    NSString *secondStr = [myDateFormatter stringFromDate:secondDayOfTheWeek];
    NSString *thirdStr = [myDateFormatter stringFromDate:thirdDayOfTheWeek];
    NSString *fourthStr = [myDateFormatter stringFromDate:fourthDayOfTheWeek];
    NSString *fifthStr = [myDateFormatter stringFromDate:fifthDayOfTheWeek];
    NSString *sixthStr = [myDateFormatter stringFromDate:sixthDayOfTheWeek];
    NSString *seventhStr = [myDateFormatter stringFromDate:seventhDayOfTheWeek];

    NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@ \n %@ \n %@", firstStr, secondStr, thirdStr, fourthStr, fifthStr, sixthStr, seventhStr);

    //log first day of week
    NSString *beginningWeek = [myDateFormatter stringFromDate:beginningOfWeek];
    NSLog(@"beginning of week is %@", beginningWeek);