Is there any efficient way to convert an NSDate to RFC 2822 Date format string ? I want to use this string to create an NSURLRequest and set the value of "If-Modified-Since" header field.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
try to use an 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];
回答2:
Swift 3
let rfcDateFormat = DateFormatter()
rfcDateFormat.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
let dateSTring = rfcDateFormat.string(from: Date())
//today result: Mon, 06 Feb 2017 17:21:18 +0100