Make sure AM / PM (Period) is not displayed in NSDateFormat stringFromDate

The Unicode Date Format Template Guide ( here ) states that adding β€œa” to the format will receive a period (for example, AM or PM), for example,

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

However, I want to make sure that the period information is not displayed, but I can not find the format string for this. The format string used 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 don’t want to just remove AM or PM from the result line, because the period syntax may differ on different calendars, etc., I just want to stop the period information from 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 create a string in the format I want, but I cannot use it to manage memory. The application I'm working on is suffering from NSDateFormatter memory leaks. Therefore, we use the singleton class to provide the installed NSDateFormatters number for an application that is never released, and therefore we minimize the number of memory leaks. Unfortunately, these static NSDateFormatters add AM / PM even when I apply the date format string, this way:

 NSDateFormatter *formatter = [MyDateFormatter dateFormat:kFormatDateMediumStyleTimeShortStyle]; [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; 
+8
ios objective-c
source share
5 answers

Here is the answer I settled on. It is a little simplified, but it does the job.

 @interface NSDateFormatter (Utils) - (NSString *)stringWithNoPeriodInformationFromDate:(NSDate*)date; @end @implementation NSDateFormatter (Utils) - (NSString *)stringWithNoPeriodInformationFromDate:(NSDate*)date { NSString *stringWithPotentialPeriodInformation = [self stringFromDate:date]; NSString *stringWithNoAMInformation = [stringWithPotentialPeriodInformation stringByReplacingOccurrencesOfString:[self AMSymbol] withString:@""]; NSString *stringWithNoPeriodInformation = [stringWithNoAMInformation stringByReplacingOccurrencesOfString:[self PMSymbol] withString:@""]; return stringWithNoPeriodInformation; } @end 
0
source share

According to the date formatter, different output on different devices working with the same version of iOS , set the local address of your NSDateFormatter to en_US_POSIX , correcting this.

Additionally, to install Local, you can avoid problems with setting the time zone, for example:
[formatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @ "UTC"]];

+5
source share

In fact, it depends on the user settings.

Refer to Fixed Formats in the Data Formatting Guide. Pay attention to this suggestion:

In iOS, a user can override the default AM / PM over a 24-hour setup time. This may cause NSDateFormatter to overwrite the format string you set.

And at the end of the paragraph:

The time view may be 13:00. However, on iOS , if the user switched the 24-hour time to shutdown , the time could be 1:00 pm.

+2
source share

You need to use posix here is the 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]; 
+1
source share

One option is to create your own NSFormatter. I did this in my code when I could not get the formatting in Xcode Interface Builder to do what I wanted. Of course, I only looked for hours, minutes and seconds, not the full date.

As for memory leaks: if possible, use ARC. If you cannot, use the Xcode Static Analyzer to try and track the wrong number of accounts.

0
source share

All Articles