-5

I have the date in the following format:

    NSString *dateString = @"1996-08-09T07:00:00Z";

I need to extract only the year from the date above:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormat setLocale:[NSLocale currentLocale]];
    [dateFormat setDateFormat:@"yyyy"];
    [dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
    NSDate *date = [dateFormat dateFromString:dateString];
    NSLog(@"date : %@", date);

Log:

    2016-02-15 06:36:47.496 App[46714:5560483] date : (null)

I even tried:

    [dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]

but no avail.

enter image description here

user1107173
  • 10,334
  • 16
  • 72
  • 117

3 Answers3

1

set date format as

                            1996-08-09T07:00:00Z
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

update

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:@"1996-08-09T07:00:00Z"];
NSLog(@"date : %@", date);
[dateFormat setDateFormat:@"yyyy"];
NSString *finalStr = [dateFormat stringFromDate:date];
NSLog(@"finalStr : %@", finalStr);

output

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

The format:

yyyy

does not match the date string:

1996-08-09T07:00:00Z

hence [NSDateFormatter dateFromString:] returned nil.

You want this format:

yyyy-MM-dd'T'HH:mm:ssZ

(Note the X specifier is documented as being the correct specifier for Z (Zulu Time) however during a discussion with @rmaddy, he has convinced me that Z is the correct one to use.)

and then to extract the year using NSDateComponents.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • not sure who gave downvoted you - sorry. But i upvoted you so you dont get a hit. – user1107173 Apr 15 '16 at 14:11
  • Do not quote the `Z` in the date format. The `Z` in the date string represents the string's timezone. If you quote the `Z` in the date format, the formatter will ignore the `Z` and interpret the date string as local time instead of UTC time. – rmaddy Apr 15 '16 at 14:49
  • @rmaddy The unquoted `Z` specifier is not [documented](http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns) to deal with an input character of `Z` (meaning *zulu time*). It supports -0800, GMT-8:00, -08:00 or -07:52:58 depending on how many `Z`s you specify. It looks like `X` does, however. – trojanfoe Apr 15 '16 at 14:56
  • I just ran a test to be 100% (I was only 99.5% sure before) and I am correct. The `Z` format specifier proper handles the `Z` timezone code as UTC time. – rmaddy Apr 15 '16 at 14:59
  • @rmaddy I am happy to accept that, however it's a bit annoying the documentation appears to be incorrect (or Apple's implementation). Anyway thanks for the correction. – trojanfoe Apr 15 '16 at 15:52
0

Try this function:

 +(NSDate *)convertUTCDateLocal:(NSString*)inputString
{
    NSString *dateFormat = @"yyyy-MM-dd'T'HH:mm:sszz";
    //for input
    NSTimeZone *inputTimeZone =[NSTimeZone timeZoneWithName:@"UTC"];
    NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
    [inputDateFormatter setTimeZone:inputTimeZone];

    //NSString *inputString = @"2015-07-31T14:00:00+0000";
    [inputDateFormatter setDateFormat:dateFormat];
    NSDate *date = [inputDateFormatter dateFromString:inputString];

    //for output
    NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
    [outputDateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [outputDateFormatter setLocale:[NSLocale currentLocale]];
    [outputDateFormatter setDateFormat: @"yyyy"];

    NSString *outputString = [outputDateFormatter stringFromDate:date];

    NSDate *outputDate = [outputDateFormatter dateFromString:outputString];

    return outputDate;
}
Suhas Arvind Patil
  • 1,732
  • 1
  • 19
  • 31