Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensure AM / PM (Period) does not appear in NSDateFormat stringFromDate

The Unicode Date Format Patterns guide (here) state that appending an 'a' to the format will get the period (AM or PM for instance), e.g.,

[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss a"];

However I wish to ensure that the period information does not appear but I cannot find a format string to do that. The format string I am using is as follows:

[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

unfortunately, when I use stringFromDate I get the following:

2013-01-09T11:11:00 AM

I dont wish to simply strip AM or PM from the resultant string because the period syntax may be different in differing Calendars etc, I just want to stop the period information appearing.

----8<------

Consider the following code

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSString *stringDate = [formatter stringFromDate:[NSDate date]];

self.labelOutput.text = stringDate;

[formatter release];

This code will produce a string in the format I want - however I cannot use it for memory management reasons. The app I am working on is plagued by NSDateFormatter memory leaks. So we use a singleton class to provide a set number NSDateFormatters to the app which are never released and therefore we minimise how much memory is being leaked. Unfortunately these static NSDateFormatters are appending AM / PM even when I apply a date format string thus:

NSDateFormatter *formatter = [MyDateFormatter dateFormat:kFormatDateMediumStyleTimeShortStyle];

[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
like image 377
Damo Avatar asked Jan 09 '13 11:01

Damo


3 Answers

According to date formatter different output on different devices running same iOS version, set the local of your NSDateFormatter to en_US_POSIX will fix this.

Additional to set Local you may wish avoid problems with time zone setting it like:
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

like image 72
Joey Avatar answered Nov 08 '22 06:11

Joey


It actually depends on user's settings.

Please see Fixed Formats part of Data Formatting Guide. Note this sentence:

In iOS, the user can override the default AM/PM versus 24-hour time setting. This may cause NSDateFormatter to rewrite the format string you set.

And at the end of the paragraph:

The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.

like image 41
Rok Jarc Avatar answered Nov 08 '22 06:11

Rok Jarc


You need to use POSIX here a sample code

  NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
      NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

      [rfc3339DateFormatter setLocale:enUSPOSIXLocale];
      [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
      [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

      // Convert the RFC 3339 date time string to an NSDate.
      NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
like image 25
Eddwin Paz Avatar answered Nov 08 '22 06:11

Eddwin Paz