gpt4 book ai didi

ios - 当系统日历是星期天时,NSCalendar 认为这是新的一周

转载 作者:行者123 更新时间:2023-11-28 21:57:42 26 4
gpt4 key购买 nike

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

NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [NSCalendar currentCalendar];

[myCalendar setLocale:[NSLocale systemLocale]];

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

[myCalendar setFirstWeekday:2];


[currentComps setWeekday:2];
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:3];
NSDate *secondDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:4];
NSDate *thirdDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:5];
NSDate *fourthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:6];
NSDate *fifthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7];
NSDate *sixthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:1];
NSDate *seventhDayOfTheWeek = [myCalendar dateFromComponents:currentComps];


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);

我的日志显示:2014-09-21 22:40:36.040 NSCalendarProject[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);

关于ios - 当系统日历是星期天时,NSCalendar 认为这是新的一周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25982866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com