0

I have this code which works perfectly on a simulator but on a real device it does not work.

The string it returns is nil, why?

-(NSString*)metodoFormatoFecha:(NSString*)cadena{

    NSString *trimmedString = [cadena stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceCharacterSet]];

    NSString *newString = [[trimmedString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];

//newString contanain -> 2015-07-28T10:41:01-04:00

    NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
    [dateStringParser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.ZZZZ"];

    NSDate *date = [dateStringParser dateFromString:newString];

    NSDateFormatter *labelFormatter = [[NSDateFormatter alloc] init];
    [labelFormatter setDateFormat:@"yyyy-MM-dd'\n'HH:mm:ss\nZZZZ"];

    NSString *FechaConFormato = [labelFormatter stringFromDate:date];

    return FechaConFormato;
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
Fabio
  • 1,913
  • 5
  • 29
  • 53
  • On your first formatter, you have extra `.` before `ZZZZ`. Is the date object `nil`, too? – Rob Jul 29 '15 at 14:24
  • possible duplicate of [What is the best way to deal with the NSDateFormatter locale "feature"?](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature) – Hot Licks Jul 29 '15 at 15:26
  • I try but not work :( – Fabio Jul 30 '15 at 18:13

1 Answers1

1

I suppose on the your device is installed AM/PM time format (depends from the locale). On the simulator is installed the "24h" time format.

You should to point the right locale, otherwise will be used the default locale (with AM/PM time format).

E.g.

#pragma mark - Lazy instantiating

- (NSDateFormatter *)getServerFormatter {
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        _serverFormatter = [NSDateFormatter new];
        _serverFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"fr_FR"];
        _serverFormatter.dateFormat = @"dd/MM/yyyy HH:mm";
    });

    return _serverFormatter;

}
Serge Maslyakov
  • 1,410
  • 1
  • 17
  • 23