Objective-C/iOS:获取一周中特定日期的日期(星期日) [英] Objective-C/iOS: Get date for specific day in week (sunday)

查看:77
本文介绍了Objective-C/iOS:获取一周中特定日期的日期(星期日)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取本周特定日期(星期日)的日期.我使用这个日期来确定是否应该进行每周重置,并且我对上次重置日期是在星期日日期之前进行了一些计算.这原则上已经被多次询问和回答,并且我的解决方案在过去 6 个月内一直有效.但是今天,也就是 2013 年 12 月 29 日,它停止工作了.

I need to get the date of a specific day (Sunday) in the current week. I use this date to determine if some weekly resetting should occur, and I do some calculations on last reset date is before sundays date etc. This has in principal been ask and answered many times, and my solution has worked for the past 6 months. But today, the 29th of December 2013, it stopped working.

目前使用的函数:

+ (NSDate *) getSundaysDate{
    NSDate *currentDate  = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setFirstWeekday:1];

    NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSWeekCalendarUnit)  fromDate:currentDate];

    NSDateComponents *dt = [[NSDateComponents alloc]init];
    [dt setWeek:[components week]];
    [dt setWeekday:1];
    [dt setMonth:[components month]];
    [dt setYear:[components year]];

    return [gregorian dateFromComponents:dt];
}

这曾经很有魅力,但今天,在我的应用程序中,这又回来了:

This used to work like a charm, but today, on my apps, this returns:

NSLog(@"sundaysDate %@", [Util getSundaysDate]);
sundaysDate 2012-12-29 23:00:00 +0000
NSLog(@"Current date %@", [[NSDate date] debugDescription]);
Current date 2013-12-29 11:37:23 +0000

注意 2012.

我知道已经在不同的上下文中多次回答了这个问题,所以我提前道歉,但我根本没有得到 2012.

I know this has been answered several times, in different contexts, so I apologize in advance, but I simply don't get the 2012.

编辑 - 只是为了阐明所需的功能:

一周从星期日开始(第 1 天)

Week starts on Sunday(day 1)

周末结束于周六(第 7 天)

Week ends on Saturday(day 7)

给定当前日期,我需要一周的第一天的日期.

Given current date I need the date of the first day of the week.

Example 1: today is 29th December 2013
Expected: Sunday is 29th December 2013

Example 2: today is 4th January 2014
Expected: Sunday was 29th December 2013

Example 2: today is 8th January 2014
Expected: Sunday was 5th January 2014

推荐答案

我认为你需要使用 WeekOfMonth 而不是 Week.
NSWeekCalendarUnit 本质上是一年中的一周,因此指定它和月份是冲突的.

I think you need to use WeekOfMonth not Week.
NSWeekCalendarUnit is essentially week of year so specifying both it and month are in conflict.

请注意,NSWeekCalendarUnit 已过时:

弃用声明
使用 weekOfYearweekOfMonth 代替,具体取决于您的意图.

Deprecation Statement
Use weekOfYear or weekOfMonth instead, depending on what you intend.

NSDate *currentDate  = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:1];

NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit)  fromDate:currentDate];
[components setWeekday:1];

NSDate *sundatsData = [gregorian dateFromComponents: components];
NSLog(@"sundaysDate: %@", sundatsData);

NSLog 输出:

sundaysDate: 2013-12-29 05:00:00 +0000

sundaysDate: 2013-12-29 05:00:00 +0000

这篇关于Objective-C/iOS:获取一周中特定日期的日期(星期日)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆