菜鸟教程小白 发表于 2022-12-12 09:33:49

ios - 如何获取给定周数和年份的第一个和最后一个日期?


                                            <p><p>我是 ios 编程新手,我拼命想用引用年份获取一周的第一个和最后一个日期。</p>

<p>我尝试构建一个方法,但确实得到了奇怪的日期。</p>

<pre><code>+ (NSArray*)getStartDateOfWeek : (NSInteger)weekNumber withYear:(NSInteger)year {
    NSMutableArray *result = [init];

    // Week Start Date

    NSCalendar *gregorianStart = [ initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsStart = [ init];


    ;
    ;

    int startDayOfWeek = [[ components:NSWeekdayCalendarUnit fromDate:] weekday];
    - ((startDayOfWeek) - 2))];

    NSDate *beginningOfWeek = ;

    // Week End Date

    NSCalendar *gregorianEnd = [ initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *componentsEnd = [init];
    ;
    ;

    int endDayOfWeek = [[ components:NSWeekdayCalendarUnit fromDate:] weekday];

    + (7 - endDayOfWeek) + 1)]; // for end day of the week

    NSDate *endOfWeek = ;

    ;
    ;

    return result;
}
</code></pre>

<p>感谢任何帮助。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>假设您想要给定一周的星期日(第一天)和星期六(最后一天),此代码将执行您想要的操作(为清楚起见,我创建了两种方法,但您可以将它们组合回一个) -</p>

<pre><code>+ (NSDate *)firstDayOfWeek:(NSInteger)weekNumber inYear:(NSInteger)year
{
    NSDateComponents *comps=[init];
    ;                                 //Change this to 2 if you want Monday rather than Sunday
    ;

    ;
    ;

    NSCalendar *cal=[ initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *newDate=;

    return newDate;

}

+ (NSDate *)lastDayOfWeek:(NSInteger)weekNumber inYear:(NSInteger)year
{
    NSDateComponents *comps=[init];
    ;                                  //Change this to 6 if you want Friday rather than Saturday
    ;

    ;
    ;

    NSCalendar *cal=[ initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *newDate=;

    return newDate;

}
</code></pre>

<p>一个重要的点是 <code>;</code> - 否则您可能会在第 1 周得到意想不到的结果 - 默认情况下,您将获得一年中的“最后”第 1 周,即也是次年的第 1 周,所以你比预期晚了一年。</p>

<p>如果您想将两者结合为一种方法,我会使用 <code>NSDictionary</code> 而不是 <code>NSArray</code> - </p>

<pre><code> +(NSDictionary *)firstAndLastDayOfWeek:(NSInteger)weekNumber inYear:(NSInteger)year
    {
      NSDateComponents *comps=[init];
      ;
      ;

      ;
      ;

      NSCalendar *cal=[ initWithCalendarIdentifier:NSGregorianCalendar];

      NSDate *firstDay=;

      ;

      NSDate *lastDay=;

      return @{@&#34;firstDay&#34;:firstDay,@&#34;lastDay&#34;:lastDay};

    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何获取给定周数和年份的第一个和最后一个日期?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23329893/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23329893/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何获取给定周数和年份的第一个和最后一个日期?