NSDate到RFC 2822日期格式

Son*_*oni 4 nsdate nsdateformatter nsurlrequest ios

有没有有效的方法将NSDate转换为RFC 2822日期格式字符串?我想使用此字符串创建NSURLRequest并设置"If-Modified-Since"标题字段的值.

tho*_*mas 16

试着用 NSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // maybe there exist a new-method now
dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
NSString *dateString = [dateFormatter stringFromDate:myDate];
Run Code Online (Sandbox Code Playgroud)

  • 额外的行是`dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];` (5认同)
  • 我想你也需要设置语言环境. (2认同)