Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDate to RFC 2822 Date format

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.

like image 788
Soni Avatar asked Dec 02 '22 00:12

Soni


2 Answers

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];
like image 94
thomas Avatar answered Dec 18 '22 00:12

thomas


Swift 3,4,5

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
like image 36
kuzdu Avatar answered Dec 18 '22 02:12

kuzdu