2011-10-20 12 views
16

ho la seguente data:Impossibile formato della data UTC con NSDateFormatter

2011-10-20T01:10:50Z 

Vorrei che fosse formattato per

"M/d/yy 'at' h:mma" 

Qui è il mio codice:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    //The Z at the end of your string represents Zulu which is UTC 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
    [dateFormatter setDateFormat:@"yyyy-dd-MM'T'HH:mm:ss'Z'"]; 
    NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:@"created_at"]]; 

    //Add the following line to display the time in the local time zone 
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"]; 
    NSString* finalTime = [dateFormatter stringFromDate:newTime]; 
    [dateFormatter release]; 
    NSLog(@"%@", finalTime);  

Sfortunatamente il tempo finale è NULL qui.

risposta

30

Si dispone di dd-MM all'indietro. Hai 10 per dd e 20 MM. Non c'è mese 20.

2011-10-20T01:10:50Z 
@"yyyy-dd-MM'T'HH:mm:ss'Z'" 

A causa di ciò, il Newtime era nullo e il finalTime era nullo.

Ecco la soluzione. Questo:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 

//The Z at the end of your string represents Zulu which is UTC 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 

NSDate* newTime = [dateFormatter dateFromString:@"2011-10-20T01:10:50Z"]; 
NSLog(@"original time: %@", newTime); 

//Add the following line to display the time in the local time zone 
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
[dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"]; 
NSString* finalTime = [dateFormatter stringFromDate:newTime]; 
NSLog(@"%@", finalTime);  

[dateFormatter release]; 

Uscite:

2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000 
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM 
+8

nome interessante per la vostra applicazione di test lì nei registri! –

+1

+1 per 'dateFormatter setTimeZone'. Questo è quello che dovevo sapere. :) – devios1

14

Questa nota tecnica ha aiutato a risolvere lo stesso problema: https://developer.apple.com/library/ios/qa/qa1480/_index.html

In poche parole, è necessario impostare 3 cose su NSDateFormatter:

  1. Locale
  2. TimeZone
  3. DateFormat

codice di esempio:

NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; 
[dateFormatter setLocale:locale]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"]; 

NSDate *date = [dateFormatter dateFromString:@"2014-10-23T01:10:50.000Z"]; 

usavo NSDateFormatter per analizzare correttamente le stringhe UTC a NSDate impostando solo fuso orario e il formato della data. Quando ha improvvisamente smesso di funzionare ho trovato questa nota tecnica e ho aggiunto setLocal: che ha risolto il problema per me.

3

Perché Swift:

let dateFormatter = NSDateFormatter() 
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC") 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" 
let dateHopefully = dateFormatter.dateFromString("2016-07-29T17:15:02Z") 

dateFormatter.timeZone = NSTimeZone.systemTimeZone() 
dateFormatter.dateFormat = "M/d/yy 'at' h:mma" 
let lastTime = dateFormatter.stringFromDate(dateHopefully!) 

print("Here is the hopeful date \(dateHopefully) and here is the lastTime \(lastTime)") 
Problemi correlati