在Objective-C中获取一年中的第一天

时间:2015-12-04 13:19:29

标签: ios objective-c nsdate

我正在尝试使用DateTools来查找周的第一天:

for (NSInteger week = 46; week <= 53; week++) {
    NSDate *tempDate = [NSDate dateWithString:[NSString stringWithFormat:@"2015-%d", (int)week] formatString:@"Y-w"];

    NSLog(@"INFO: tempDate: %@, day: %.2d, week: %d", tempDate, (int)[tempDate day], (int)week);
}

for (NSInteger week = 1; week <= 5; week++) {
    NSDate *tempDate = [NSDate dateWithString:[NSString stringWithFormat:@"2016-%d", (int)week] formatString:@"Y-w"];

    NSLog(@"INFO: tempDate: %@, day: %.2d, week: %d", tempDate, (int)[tempDate day], (int)week);
}

我得到了这个输出:

INFO: tempDate: 2015-11-07 22:00:00 +0000, day: 08, week: 46
INFO: tempDate: 2015-11-14 22:00:00 +0000, day: 15, week: 47
INFO: tempDate: 2015-11-21 22:00:00 +0000, day: 22, week: 48
INFO: tempDate: 2015-11-28 22:00:00 +0000, day: 29, week: 49
INFO: tempDate: 2015-12-05 22:00:00 +0000, day: 06, week: 50
INFO: tempDate: 2015-12-12 22:00:00 +0000, day: 13, week: 51
INFO: tempDate: 2015-12-19 22:00:00 +0000, day: 20, week: 52
INFO: tempDate: 2015-12-26 22:00:00 +0000, day: 27, week: 53
INFO: tempDate: 2015-12-26 22:00:00 +0000, day: 27, week: 1
INFO: tempDate: 2016-01-02 22:00:00 +0000, day: 03, week: 2
INFO: tempDate: 2016-01-09 22:00:00 +0000, day: 10, week: 3
INFO: tempDate: 2016-01-16 22:00:00 +0000, day: 17, week: 4
INFO: tempDate: 2016-01-23 22:00:00 +0000, day: 24, week: 5

如您所见,来自53的{​​{1}}周与来自2015 1周相同(This site告诉我们我2015年有53周的时间)

实际上,来自2016的{​​{1}}周开始于1

另外,请注意2016给了我一周第一天的前一天。这是为什么?我可以简单地使用04.01.2016,但我不知道它是否是hackish,问题应该在其他地方解决。

我尝试使用dateWithString:formatString:作为@DarkDust提到,但无济于事。所以这个:

dateByAddingDays:1

给了我这个:

NSDateComponents

以下是for (NSInteger week = 1; week <= 5; week++) { NSDate *tempDate = [NSDate dateWithString:[NSString stringWithFormat:@"2016-%d", (int)week] formatString:@"Y-w"]; NSDateComponents *dateComponents = [NSDateComponents new]; dateComponents.year = 2016; dateComponents.weekOfYear = week; NSLog(@"INFO: tempDate: %@, day: %.2d, week: %d = %@", tempDate, (int)[tempDate day], (int)week, [calendar dateFromComponents:dateComponents]); } 'INFO: tempDate: 2015-12-26 22:00:00 +0000, day: 27, week: 1 = 2015-12-31 22:00:00 +0000 INFO: tempDate: 2016-01-02 22:00:00 +0000, day: 03, week: 2 = 2015-12-31 22:00:00 +0000 INFO: tempDate: 2016-01-09 22:00:00 +0000, day: 10, week: 3 = 2015-12-31 22:00:00 +0000 INFO: tempDate: 2016-01-16 22:00:00 +0000, day: 17, week: 4 = 2015-12-31 22:00:00 +0000 INFO: tempDate: 2016-01-23 22:00:00 +0000, day: 24, week: 5 = 2015-12-31 22:00:00 +0000 实施:

DateTools

重复报告:

  

排名前3的是错误的,其中2个是downvotes,他们根本不做我需要的。

2 个答案:

答案 0 :(得分:0)

这是一个坏主意,你在做什么。您必须了解日期是时间戳,可以根据您使用的时区和日历进行不同的解释。正如评论中已经提到的那样,您应该为解决方案使用日期组件。请注意,打印日期可能使用不同的格式,结果可能不会出现。

接下来,一年中的第一个工作日取决于定义。如果我没记错的话,一些标准(或所有标准)将根据一周的哪一天是第一天来处理一年中的第一周。换句话说,如果1.1是星期日,那么今年的第一周是在12月,但如果是星期二那么它是在1月。

因此,如果你想找到给定年份的第一个星期一的开头,代码应该看起来像这样(我没有测试它):

+ (NSDate *)firstWeekInYear:(NSInteger)year {

        NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

        NSDate *toReturn = nil;
        NSDateComponents *components = [[NSDateComponents alloc] init];
        components.year = year;
        NSDate *beginningOfTheYear = [calendar dateFromComponents:components];
        NSInteger day = [calendar component:NSCalendarUnitWeekday fromDate:beginningOfTheYear];
        NSInteger daysToAdd = (8-day)%7;

        toReturn = [calendar dateByAddingUnit:NSCalendarUnitDay value:daysToAdd toDate:beginningOfTheYear options:kNilOptions];

        return toReturn;
    }

因此,您需要选择日历,创建具有目标年份的日期组件,使用日历从这些组件中获取日期以获得年初。然后找出工作日是什么,并增加这么多天,结果是一年中第一个星期一的开始。

答案 1 :(得分:0)

我终于明白了。 @Micic的解决方案在iOS 7中不起作用而且很难理解,因此我设法找到了一个适用于iOS 7的更简单的解决方案:

+ (NSDate *)getFirstDayInWeek:(NSInteger)week ofYear:(NSInteger)year {

    /* create the calendar only once */

    static NSCalendar *calendar = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

        calendar.firstWeekday = 2;
        calendar.minimumDaysInFirstWeek = 4;
    });

    NSDateComponents *components = [NSDateComponents new]; // create an empty date components object

    components.year = year;
    components.weekOfYear = week; // set the number of the week in the year
    components.weekday = calendar.firstWeekday; // important! set the result date's day of week

    return [calendar dateFromComponents:components];
}