gpt4 book ai didi

iphone - 如何根据 objective-c 中的月份创建周列表?

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:43 25 4
gpt4 key购买 nike

我需要根据月份创建列表周,我尝试了以下代码但我无法得到正确的答案。任何人都为我的以下输出提供正确的解决方案:

NSDate *weekDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfMonthCalendarUnit| NSWeekdayCalendarUnit) fromDate:weekDate];
for (int i = 0; i <=10 ;i++)
{
[currentComps setWeekOfMonth:i];

[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 = @"'week' w dd/MM";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:lastDayOfTheWeek];

NSLog(@"first - %@ \nlast - %@", firstStr, secondStr);
}

我得到了这个输出:第一 - 第 4 周 19/01最后 - 第 4 周 25/01 第一 - 第 5 周 26/01最后 - 第 5 周 01/02 第一周 6 02/02上周 6 08/02 第一周 7 09/02最后 - 第 7 周 15/02第一 - 第 8 周 16/02最后 - 第 8 周 22/02第一周 9 23/02上周 9 01/03第一周 10 02/03上周 10 08/03第一周 11 09/03最后 - 第 11 周 15/03第 12 周 16/03最后 - 第 12 周 22/03 第 13 周 23/03上周 13 年 29 月 03 日 第一周 14 30/03最后 - 第 14 周 05/04

但是我不需要前两周,因为我需要基于月份的当前周到十周列表之后,但是这里的结果是在两周列表之前的,我当前的一周(第一周 - 第 6 周 02/02last - week 6 08/02) 所以任何人都可以帮助我。

最佳答案

最后我在 NSCalendar 中得到了一周的开始日期和结束日期的结果周列表。

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
// Create the start date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
for(int currentdateindexpath=0;currentdateindexpath<=10;currentdateindexpath++)
{
[oneDayAgoComponents setWeek:currentdateindexpath];
NSDate *monthAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
[formatter setDateFormat:@"'week'W"];
NSString *stringFromDate = [formatter stringFromDate:monthAgo];

NSLog(@"seg %d",statusseg.selectedSegmentIndex);
//NSDate *today = [[NSDate alloc] init];
//NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

// Get the weekday component of the current date
NSDateComponents* subcomponent = [calendar components:NSWeekdayCalendarUnit
fromDate:[NSDate date]];

/*
Create a date components to represent the number of days to subtract from the current date.
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today is Sunday, subtract 0 days.)
*/
// NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];

NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date] options:0];
NSLog(@" beginningOfWeek %@",beginningOfWeek);
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
NSLog(@"weekstartdate %@",weekstartdate);
/*
Optional step:
beginningOfWeek now has the same hour, minute, and second as the original date (today).
To normalize to midnight, extract the year, month, and day components and create a new date from those components.
*/

NSDateComponents *components =
[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit) fromDate: beginningOfWeek];
beginningOfWeek = [calendar dateFromComponents:components];
NSLog(@"Show %@",beginningOfWeek);
//+++++++++++++++++++ start week+++++++++++++++
[oneDayAgoComponents setDay:7- ([subcomponent weekday])];

NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date] options:0];
NSLog(@" endOfWeek %@",endOfWeek);
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
NSLog(@"weekendtdate %@",weekendtdate);
_topdate_lbl.text=[NSString stringWithFormat:@"%@ %@ to%@ ",stringFromDate,weekstartdate,weekendtdate];
}

关于iphone - 如何根据 objective-c 中的月份创建周列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21571137/

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