Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape NSDateFormatter String

How to escape characters from NSDateFormatter format?

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);
like image 586
Luciano Nascimento Avatar asked May 15 '13 16:05

Luciano Nascimento


1 Answers

You can escape text by enclosing it in single quotes:

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

From the 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
like image 132
vikingosegundo Avatar answered Oct 15 '22 00:10

vikingosegundo