2013-05-15 13 views
9

Jak uniknąć znaków z formatu NSDateFormatter?Escape NSDateFormatter String

NSDate *currentDate = [NSDate date]; 
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; 
[dateFormater setDateFormat:@"dd/MM/yyyy \\a\\t HH:mm"]; 
NSString *dateFormated = [dateFormater stringFromDate:currentDate]; 

NSLog(@"%@", dateFormated); 
+5

zakładek [data formatujących] (https://developer.apple. com/library/ios/# documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) strona w dokumentach, a także [specyfikacja formatu daty IEEE] (http://www.unicode.org/reports/tr35/ tr35-25.html # Date_Format_Pattern s). Każdy, kto pracuje z 'NSDateFormatter' powinien przeczytać te strony. – rmaddy

Odpowiedz

26

można uciec tekst poprzez zamknięcie go w apostrofach:

[dateFormater setDateFormat:@"dd/MM/yyyy 'at' HH:mm"]; 

Z docs:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"]; 
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000]; 
NSString *formattedDateString = [dateFormatter stringFromDate:date]; 
NSLog(@"formattedDateString: %@", formattedDateString); 
// For US English, the output may be: 
// formattedDateString: 2001-01-02 at 13:00