20

I'm initializing my NSDateFormatter thusly:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];

dateString is now:

Thu, 29 Jul 2010 14:58:42 GMT+00:00

I want to get rid of the "+00:00"

I'm guessing from http://unicode.org/reports/tr35/tr35-6.html#Time_Zone_Fallback that I might have a localization issue. I'm working around this right now by removing the "+00:00" manually, but that isn't ideal.

EDIT

I tried a couple of new ways to create the NSTimeZone, but they both produce the same dateString:

[NSTimeZone timeZoneWithName:@"GMT"];
[NSTimeZone timeZoneWithName:@"UTC"];
[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[NSTimeZone timeZoneWithAbbreviation:@"UTC"];
Heath Borders
  • 30,998
  • 16
  • 147
  • 256
  • 1
    +1 for the interesting question. I can't figure it out myself. BTW, you might want to know about a possible memory leak using the 'z' specifier: http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/ – Shaggy Frog Sep 14 '10 at 19:48

2 Answers2

23

Remove the trailing 'z' character from the format string if you don't want to display the time zone.

EDIT

On the other hand, if you just want to display the timezone name, just make the 'z' uppercase. ((edit: leave the 'z' lowercase for named timezone, i.e. PST and uppercase 'Z' for -0800))

EDIT

Lowercase 'z' works fine for all the other timezones, but unfortunately GMT is a special case. So the easiest thing to do is to just omit the 'z' and append " GMT" to the formatted date.

Dickey Singh
  • 713
  • 1
  • 8
  • 16
jlehr
  • 15,557
  • 5
  • 43
  • 45
  • The OP wants to keep the time zone abbreviation, but get rid of the +00:00 bit. – Shaggy Frog Sep 14 '10 at 19:31
  • Uppercase 'Z' yields "+0000" per RFC 822 standards. I just want "GMT" – Heath Borders Sep 15 '10 at 18:59
  • Gah, just plain lowercase 'z' works fine for all timezones other GMT, which is a special case. I think the easiest thing to do would be to omit the 'z' and just append " GMT" to the formatted date. It's really annoying that (unless I'm misreading it) the spec doesn't provide a straightforward way to do this. – jlehr Sep 15 '10 at 19:48
  • That's what I'm doing. If you edit your answer to reflect your previous statement, I'll mark this as answered. – Heath Borders Feb 14 '11 at 05:34
  • Heres a gist, all about the 'z' and 'v' formatters: https://gist.github.com/rwyland/8683010 – rwyland Jun 18 '14 at 15:38
4

Accepted answer had a typo.

On the other hand, if you just want to display the timezone name, just make the 'z' uppercase.

leave the 'z' lowercase for named timezone, i.e. PST and uppercase 'Z' for -0800

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMMM dd, yyyy (EEEE) HH:mm:ss z Z"];
    NSDate *now = [NSDate date];
    NSString *nsstr = [format stringFromDate:now];

//January 23, 2013 (Wednesday) 12:33:46 PST -0800

Dickey Singh
  • 713
  • 1
  • 8
  • 16