NSDate to RFC 2822 Date format

冷暖自知 提交于 2019-12-07 11:22:07

问题


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


来源:https://stackoverflow.com/questions/8636754/nsdate-to-rfc-2822-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!