简体   繁体   中英

Strange NSDate and NSDateFormatter behaviour

I am trying to get date for week of this year. this is my code and also a runtime value that is being calculated:

在此输入图像描述

Why is the year not correct? Tried other formats as well, like yw and all gave me same results. This issue does fix itself if i use YYYY-w BUT then i get wrong month/date since the year is calculated differently then. Just for information this is what i get with YYYY:

在此输入图像描述

Now the date 04-29 is the correct one for week 18 in "yyyy" case (see here: http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php ) but the year is correct only when "YYYY" is used (date is wrong then).

So whats up with that?

-------- UPDATE 1 ---------

After adding solution as offered in one of answers:

在此输入图像描述

-------- UPDATE 2 ----------

So my date 2000 is the same value as "no year defined" (previously it was 1970). See here: http://openradar.appspot.com/12358210

-------- UPDATE 3 ----------

System information (for those that will try replicating this).

XCode: Version 4.6.2 (4H1003) iOS: 6.1 OSX: 10.8.3

-------- Update 4 ----------

iOS 5.1 provides different results when using this:

在此输入图像描述

As if iOS 5.1 does not support "w" in NSDateFormatter.

EDIT 1 : this answer doesn't explain the results OP is seeing. See comments discussion for more info

EDIT 2 : Very peculiar results on my machine. On iOS 6, week doesn't respect first day of week setting in System Preferences at all:

iOS 6 always returns:

2000-1 -> 1st Jan, 2000 (Saturday)

2001-1 -> 6th Jan, 2001 (Saturday)

Even if I do [calendar setFirstWeekday: 3] and [dateFormatter setCalendar: calendar] , doesn't affect the results at all (unlike iOS 5)! This is certainly a bug in iOS 6 NSCalendar API. I would recommend using NSDateComponents or calculating the date yourself. It should be easy given the info in this answer - you can assume ISO-8601 (minimum 4 days in first week and week starts with Monday), unless of course, you can ask user for their preference.


ORIGINAL ANSWER


Here's what's happening:

In your locale, minimum number of days in a week is 5 and week starts on a Saturday.

Assuming this is true, first week for you would be 5th Jan (Sat) - 11th Jan 2013 . And 18th week would then be, you guessed it: 4th May 2013 .

Why? Because Apple follows Unicode Technical Standard #35 version tr35-25 (on iOS 6+).

F.4 Week of Year

Values calculated for the Week of Year field range from 1 to 53 for the Gregorian calendar   
(they may have different ranges for other calendars). Week 1 for a year is the first week 
that contains at least the specified minimum number of days from that year. Weeks between 
week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 
or 53 (if needed). For example, January 1, 1998 was a Thursday. If the first day of the 
week is MONDAY and the minimum days in a week is 4 (these are the values reflecting ISO 
8601 and many national standards), then week 1 of 1998 starts on December 29, 1997, and 
ends on January 4, 1998. However, if the first day of the week is SUNDAY, then week 1 of 
1998 starts on January 4, 1998, and ends on January 10, 1998. The first three days of 1998 
are then part of week 53 of 1997.

Also, YYYY-w is the right format.


if you are using NSCalender then you'l get all the compontens like this,

NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
    NSDateComponents *components1 = [calendar components:units fromDate:date];
    NSInteger year = [components1 year];
    NSInteger week=[components1 weekOfYear];

Can you try with this

NSString *dateString = @"2013-18";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-ww"];

NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"Date: %@",date);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM