【问题标题】:Escape NSDateFormatter String转义 NSDateFormatter 字符串
【发布时间】:2013-05-15 16:43:30
【问题描述】:

如何从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);

【问题讨论】:

标签: objective-c nsdate nsdateformatter


【解决方案1】:

您可以通过将文本括在单引号中来转义:

[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

【讨论】:

  • 如果要转义单引号,请在转义文本中输入两次单引号 (' it''s ')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多