如何从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);
解决方法
您可以通过将其包含在单引号中来转义文本:
[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