NSDate to RFC 2822 Date format - ios

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.

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];

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

Related

how do i convert a string to specific format

I have referred lot of links but could not find the solution hence asking it again.I have string which i need to convert into date format. Below is example
Input string : "1410271500"
In above string 14 is for 2014
10 is for Oct
27 is day
1500 is 3:00 PM
Output string : "2014-10-27 03:00:00"
Input string in not in long-integer format.
Thanks
do like
NSString *getMilliSec = #"1410271500";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyMMddHHmm"];
NSDate *getDate = [formatter dateFromString:getMilliSec];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"]; // or use yyyy-MM-dd hh:mm:ss
formatter.timeZone = [NSTimeZone systemTimeZone]; // optional
NSString *stringFromDate = [formatter stringFromDate:getDate];
swift3
let getMilliSec = "1410271500"
let formatter = DateFormatter()
formatter.dateFormat = "yyMMddHHmm"
let getDate = formatter.date(from: getMilliSec)
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss a" // or use yyyy-MM-dd hh:mm:ss
formatter.timeZone = NSTimeZone.system // optional
var stringFromDate = formatter.string(from: getDate!)
output

Convert date string into proper format

I am getting a response from a server and there is a date string I need to convert into a date:
Thu Jun 29 07:15:25 +0000 2017
I am trying to convert the string into a human readable format. Can anyone suggest how to convert this string into date?
You need to parse the date string into a Date object using a DateFormatter, then create a string from that Date using another DateFormatter with the output format you want to use.
let created_at = "Thu Jun 29 07:15:25 +0000 2017"
let df = DateFormatter()
df.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
guard let date = df.date(from: created_at) else {return}
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = "EEE, MMM d"
let outputDate = outputFormatter.string(from: date)
If you want to know what date formats look like, use NSDateFormatter.com, which has an interactive interface where you can play around with different date formats and have examples of most common formats as well.
This is how you can format it in "Thu Jun 29"
-(NSString *)formatCreatedAt:(NSString *)createdAt
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:createdAt];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd"];
NSString *stringFromDate = [formatter stringFromDate:date];
return stringFromDate;
}
Pass the value of created_at in this method and the output will be returned in your specified format.

NSDate give me the error date, Anybody know why?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE MMM DD HH:mm:ss Z yyyy";
NSLog(#"Original Time:%#", _created_at);
NSDate *newCreatedDate = [dateFormatter dateFromString:_created_at];
NSString *newString = [dateFormatter stringFromDate:newCreatedDate];
NSLog(#"Conversion Time:%#", newString);
This is the result:
Original Time:Sun Oct 25 18:37:03 +0800 2015
Conversion Time:Sun Jan 25 18:37:03 +0800 2015
You should be using dd instead of DD.
dateFormatter.dateFormat = #"EEE MMM dd HH:mm:ss Z yyyy";

Date formate to convert NSString (Wed Jul 29 14:46:11 +0000 2015) to NSDate in ios

I have date in NSString format as "Wed Jul 29 14:46:11 +0000 2015". I have tried all possible ways of converting this string into NSDate. But couldn't find the right format for type of string.
Can someone help out or give suggestion to convert this into NSDate.
Below is the code:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"EEEE MM dd HH:mm:ss Z YYYY";
//[format setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"];
[format setLocale:locale];
NSDate *date = [format dateFromString:#"Wed Aug 19 17:36:46 +0000 2015"];
NSLog(#"converted date is %#",date);*
But its returning wrong date as converted date is 2014-12-24 17:36:46 +0000
Thanks.
You need to set your NSDateFormatter's dateFormat to EEEE MM dd HH:mm:ss Z YYYY.
You can use the following code for converting the specified string to date.
Objective C:
NSString *string = #"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"EEEE MMM dd HH:mm:ss Z yyyy";
NSDate *date = [format dateFromString:string];
Swift:
var dateStr = "Wed Jul 29 14:46:11 +0000 2015"
var formatter = NSDateFormatter();
formatter.dateFormat = "EEEE MMM dd HH:mm:ss Z yyyy"
var date = formatter.dateFromString(dateStr)
Are you sure that:
you have the right format for NSDateFormatter when calling dateFromString()?
you have the right locale?
you have looked up any errors returned?
Please post the code and your failure message/ results.
See documentation and related SO post
You can follow this. It helps you.
NSString *dateString = #"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE MMMM dd yyyy HH:mm:ss Z yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];

Is it possible to parse "Fri, 24 Feb 2012 20:00:00 GMT" with using NSDateFormatter?

I'm trying to parse
Fri, 24 Feb 2012 20:00:00 GMT
Is it possible to do this with using NSDateFormatter?
Try the following:
NSString *dateString = #"Fri, 24 Feb 2012 20:00:00 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss zzz";
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
Depending on how you will specify the time zone, you may have to change the line:
dateFormatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss zzz";
with
dateFormatter.dateFormat = #"EEE, d MMM yyyy HH:mm:ss Z";
if you will use GMT-02:00 for example, but keep it if you will use PDT.
Sure you should use a mask..
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] ];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *date = [dateFormatter dateFromString:dateString];
It could work.

Resources