如何从NSDateFormatter
格式转义字符?
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);
答案 0 :(得分:29)
您可以通过将文本括在单引号中来转义文本:
[dateFormater setDateFormat:@"dd/MM/yyyy 'at' HH:mm"];
来自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