2012-12-21 33 views
0

我嘗試使用下面的代碼無法使用dateFromString

NSString *origDate = @"2012-12-06T09:27:18+08:00"; 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setFormatterBehavior:NSDateFormatterBehavior10_4]; 
[df setDateFormat:@"yyyy-mm-dd HH:mm:ss VVVV"]; 
NSDate *convertedDate = [df dateFromString:origDate]; 

的時間字符串轉換但是當我打印convertedDate,它返回我空轉換原來的日期。我的猜測是,我使用的日期格式不匹配。我如何修改代碼以使其工作?我可以使用什麼格式來匹配我的字符串?

編輯

(指蘋果的文件之後)我檢查了蘋果公司的網頁上的日期格式的文檔,發現下面的代碼

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]; 

以上格式都是符合我在原來的日期字符串「2012-12-06T09:27:18 + 08:00」。不過,我仍然收到一個空值。我靠近了嗎?我還可以如何更新?

+0

你的猜測是正確的。源日期字符串看起來很徹底;你有任何控制權?似乎在日期和時間以及時區之間應該有空格。但除此之外,是的,您必須更改您爲日期格式化程序指定的字符串。 – trudyscousin

+0

@trudyscousin它是由我的系統創建的,但我確信我可以操縱原始字符串。但是你說沒有可用於轉換當前字符串的'setDateFormat'? – Zhen

+0

不,完全沒有。蘋果文檔中的[本頁](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1)將引導您正確地處理字符串所需的文檔。查看「固定格式」部分。 – trudyscousin

回答

0

根據您的原始輸入,提供給您的日期格式此格式字符串應該把工作做好:

@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ" 

注:我已經在Mac OS X 10.8.2測試這一點。

+0

如果它是iOS 6 .. –

+0

是的,OS X> = 10.8 –

0

格式字符串將在iOS6上解析(不是iOS5 - > nil),但它對輸出沒有用處,因爲解析日期將失去它的時區信息。 iOS6中的輸出可能類似於「2012-12-06T17:27:18Z」,可能這取決於時區設置爲GMT。

我的代碼:

static NSDateFormatter *gXmlDateFormatter = nil; 
// lazy init 
+ (NSDateFormatter *)xmlDateFormatter 
{ 
    // e.g. updateDateTime="2012-09-18T11:06:19+00:00" 
    if (gXmlDateFormatter == nil) { 
     // prepare for parsinf Arinc-ISO-XML-dates input 
     gXmlDateFormatter = [[NSDateFormatter alloc] init]; 
     gXmlDateFormatter = [NSTimeZone timeZoneForSecondsFromGMT:0]; 
     gXmlDateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 

     gXmlDateFormatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ"; // only parsing! in iOS 6 (iOS5 will parse nil) 
//  gXmlDateFormatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";  // all iOS but with NO colons in timeZone (add/remove) 
    } 
    NSLog(@"gXmlDateFormatter:%@",gXmlDateFormatter); 
    return gXmlDateFormatter; 
} 
// there's a problem with the above dateformater and iOS5 creating nil-results 
+ (NSDate *)dateFromXMLString:(NSString *)arincDateString 
{ 
    NSString *dateString = arincDateString; 

    // xmlDateStrings may contain a ':' in the timezone part. iOS and Unicode DO NOT 
    // so always remove the xml-standard colon ':' from the timezone to make it iOS/Unicode compatible 
    // xml: http://www.w3schools.com/schema/schema_dtypes_date.asp 
    // iOS: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns) 
    NSRange zRange = NSMakeRange(arincDateString.length-3, 1); 
    dateString = [arincDateString stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:zRange]; 

    NSDate *date = [self.arincDateFormatter dateFromString:dateString]; 

    if(!date)NSLog(@"PARSING arincDateString:'%@' -> (NSDate*)%@ ",arincDateString,date); 

    return date; 
} 
+ (NSString *)xmlStringFromDate:(NSDate *)date 
{ 
    if(!date) return nil;  // exit on nil date 

    @autoreleasepool { 
     NSString *dateString = [self.arincDateFormatter stringFromDate:date]; 

     // iOS5 does not use a ':' in the timeZone part but xml needs it 
     // so allways add the xml-standard colon ':' into the timezone 
     NSMutableString *string = [NSMutableString stringWithString:dateString]; 
     if(22 < string.length) {  // prevent crashing 
      [string insertString:@":" atIndex:22]; 
     } else { 
      NSLog(@"date output string too short:%d<22",string.length); 
     } 
     dateString = string; 

     if(!dateString) 
      NSLog(@"OUTPUT '%@' -> (NSString*)%@",date,dateString); 
     return dateString; 
    } 
}