Pergunta

+(NSDate *)DateServerFormatFromString:(NSString *)date
{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate* returnDate = [dateFormatter dateFromString:date];
return returnDate;

}

The function returns date in format "2013-05-09 08:06:04 AM +0000". But i want it in 24 hour format. The date being passed to this function is in the exact same format as given in setDateFormat method. The device's region format is set to "United Kingdom" and time format is set to "12 hour format". This should not be changed. When time format is set to 24 hour format in the device, the function works perfectly. What is wrong with the code. I am using iPad 1 with OS 5.1.1. Setting locale identifer or even timezone didnt make a difference. Thanks in advance.

Foi útil?

Solução

This is not necessarily an answer, just an explanation, found in the Apple docs (here):

Although in principle a format string specifies a fixed format, by default NSDateFormatter still takes the user’s preferences (including the locale setting) into account. You must consider the following points when using format strings:

  • NSDateFormatter treats the numbers in a string you parse as if they were in the user’s chosen calendar. For example, if the user selects the Buddhist calendar, parsing the year 2010 yields an NSDate object in 1467 in the Gregorian calendar. (For more about different calendrical systems and how to use them, see Date and Time Programming Guide.)

  • 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.

Outras dicas

The problem here is how you're getting the string. You say you got it

by printing the description of returnDate

However, according to the documentation, the -description method says this:

The representation is useful for debugging only.

There are a number of options to aquire a formated string for a date including: date formatters (see NSDateFormatter and Data Formatting Guide), and the NSDate methods descriptionWithLocale:, dateWithCalendarFormat:timeZone:, and descriptionWithCalendarFormat:timeZone:locale:

What this means is that the value returned by -description does not respect any 12- or 24-hour time settings. It's just a debug version.

If you want to express a date as a human-readable string, you must use an NSDateFormatter.

Again and again:

Did you NSLog() the date? Logging a date is always been done in a normalized standard format. If you want to log a date formatted, log the result of a formatter, not the date directly.

You want 24h format for what? Format is used to convert NSDate to NSString with correct string format. Just make reverse converting when you need it

12 hour format

[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss"];

24 hour format

[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top