簡體   English   中英

如何根據目標c中的月份創建周列表?

[英]how to create a weeks list based on month in objective c?

我需要根據月份創建列表周,我已經嘗試了以下代碼,但我無法得到正確答案。任何一個給出以下輸出的正確解決方案:

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/04/04周

但我不需要前兩周,因為我需要當前一周到十個星期后的月份列表,但是這里結果來了兩個星期之前列表也來了我的當前一周(第一周 - 第6周02/02最后一周 - 第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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM