iOS 6 Nsdateformatter

iOS 6 NSDateFormatter stringFromDate: empty day of week

That actually looks like a bug in NSDateFormatter on iOS 6. However, it seems that
the bug occurs only if you explicitly set

[formatter setDoesRelativeDateFormatting:NO];

If you omit that line (and NO is the default anyway) then the result is OK (at least when I tested it).

Remark: I assume that setDoesRelativeDateFormatting is only meant to be used in connection
with setDateStyle:, but that is pure guessing.

iOS6 NSDateFormatter default year

Yes I just noticed this issue. Apparently this bug is reported at Apple, see http://openradar.appspot.com/12358210


Edit: this is how I dealt with this issue in a project I'm working on...

// I use the following code in a category to parsing date strings

- (NSDate *)dateWithPath:(NSString *)path format:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter.dateFormat = format;

NSString *string = [self valueWithPath:path];
return [formatter dateFromString:string];
}

// old parsing code that worked fine in iOS 5, but has issues in iOS 6

NSDate *date            = [element dateWithPath:@"datum_US" format:@"yyyy-MM-dd"];
NSDate *timeStart       = [element dateWithPath:@"aanvang" format:@"HH:mm:ss"];
NSTimeInterval interval = [timeStart timeIntervalSince1970];
match.timeStart         = [date dateByAddingTimeInterval:interval];

and the fix ...

// the following code works fine in both iOS 5 and iOS 6

NSDate *date = [element dateWithPath:@"datum_US" format:@"yyyy-MM-dd"];
NSDate *timeStart = [element dateWithPath:@"aanvang" format:@"HH:mm:ss"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *timeComps = [calendar components:units fromDate:timeStart];
match.timeStart = [calendar dateByAddingComponents:timeComps toDate:date options:0];

NSDateFormatter show wrong year

YYYY is not the same as yyyy.

According to this page which the iOS Date format page references;

`y`: Year
`Y`: Year (in "Week of Year" based calendars). This year designation is used in
ISO year-week calendar as defined by ISO 8601, but can be used in
non-Gregorian based calendar systems where week date processing is desired.
May not always be the same value as calendar year.

The operative sentence being the last one. Use yyyyinstead.


Further details on how and why the year values may deviate when using YYYY:

The ISO week-numbering year starts at the first day (Monday) of week
01 and ends at the Sunday before the new ISO year (hence without
overlap or gap). It consists of 52 or 53 full weeks. The ISO
week-numbering year number deviates from the number of the traditional
Gregorian calendar year on a Friday, Saturday, and Sunday, or a
Saturday and Sunday, or just a Sunday, at the start of the traditional
Gregorian calendar year (which are at the end of the previous ISO
week-numbering year) and a Monday, Tuesday and Wednesday, or a Monday
and Tuesday, or just a Monday, at the end of the traditional Gregorian
calendar year (which are in week 01 of the next ISO week-numbering
year). For Thursdays, the ISO week-numbering year number is always
equal to the traditional Gregorian calendar year number.

Examples:

Monday 29 December 2008 is written "2009-W01-1"

Sunday 3 January 2010 is written "2009-W53-7"

From https://en.wikipedia.org/wiki/ISO_8601#Week_dates
(bold styling added)

NSDateFormatter Issue with iOS6?

Try the following (include the timezone string):

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy/MM/dd HH:mm:ss Z"];
NSDate *fromDate = [fmt dateFromString:@"2012/10/11 21:46:31 +0000"];

NSDateFormatter iOS6 issue

I think it's a bug. I ran into same issues with different behaviours between iOS 5 and 6.

Check this out:

http://openradar.appspot.com/12385667

http://openradar.appspot.com/12358210

Maybe you're into one of these.

iOS 6.1 NSDateFormatter has changed since iOS 5 - broken parsing?

The format styles should only be used to convert NSDate objects to text to display to the user. When parsing a date string in a known format, you must use a specific format, never the styles. The use of the en_US_POSIX locale is used to ensure that the format you specify isn't tweaked by the OS based on user preferences such as the 24-hour time setting.

So, as you suspected, you need to remove the two calls to set the date and time styles and replace them with a call to set a specific format that matches your known date/time string you need to parse.

Behavior of NSDateFormatter in iOS 5.1 and iOS 6

Replace @"MM/dd/yyyy HH:mm:ss (vvvvv)"; with @"MM/dd/yyyy HH:mm:ss ('GMT'Z)";

The Z format string will output what you were doing with vvvvv except for the GMT prefix and there is no colon as well.

If the colon is needed, you can add this line to add it to the string:

sDate = [NSString stringWithFormat:@"%@:%@", [sDate substringToIndex:sDate.length-3], [sDate substringWithRange:NSMakeRange(sDate.length-3, 3)]];


Related Topics



Leave a reply



Submit