How to parse the timezone in this date string? - ios

I would like to convert this string to NSDate: "Sun, 08 Mar 2015 10:32:12 -0000"
This is what I got working:
NSDateFormatter *dateformatter = [NSDateFormatter new];
[dateformatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss"];
NSDate *date = [dateformatter dateFromString:#"Sun, 08 Mar 2015 10:32:12"];
But I don't know how to parse the -0000, if its part of the string, the date would fail.

You need to add a 'Z' to handle the timezone piece of the date string.
Example:
NSDateFormatter *dateformatter = [NSDateFormatter new];
[dateformatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateformatter dateFromString:#"Sun, 08 Mar 2015 10:32:12 -0000"];

Related

ios NSDateFormatter date from string using literal date

I am trying to create a date out of my date string that goes:
Tue, 29 Jul 2014 17:22:30 +0200
I am using this code but it still gives me null as I try to dateFromString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd LLL YYYY hh:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:bazosItem.itemDate]);
so the output is:
The date: (null)
I have been searching for the right dateFormat in the internet and have rechecked it multiple times and I have no idea what is going wrong with the date.
There are some mistakes in your format, first the YYYY should be yyyy second the hh is for AM/PM hour not 24 hours for this you should use HH.
Third and most important you should the the date format in which language the date is by adding a locale:
NSString *temp = #"Tue, 29 Jul 2014 17:22:30 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:temp]);
Your date format seems to be incorrect. Try the following
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0200"];

NSDateFormatter not recognizing dates

I have an array that contains some date strings. I'm using NSDateFormatter but the problem is that it recognizes some of the dates and do not recognize others while all of them have the same format!
for example it formats: Sat, 01 Feb 2014 08:44:00 +0430
But returns null for: Wed, 29 Jan 2014 17:40:00 +0430
this is the code i used:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss xx"];
NSDate *date = [dateFormat dateFromString:dateInput];
Use this date format [dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss xx"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss xx"];
NSDate *date = [dateFormat dateFromString:dateInput];
Note:
HH is denoted by 24 hour format
hh is is denoted by 12 hour format
Try this
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss xx"];
Because, HH is syntax for 24 hour format which is suitable for your criteria.

How to set NSDateFormatter to parse "..GMT+0000 (UTC)"

A server is sending a date in this format: "Fri Dec 13 2013 16:26:18 GMT+0000 (UTC)" but I can't get it to parse using NSDateFormatter
If I do this this then it parses:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:#"Fri Dec 13 2013 16:26:18"];
However I need this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd yyyy HH:mm:ss XXX"];
NSDate *date = [dateFormatter dateFromString:#"Fri Dec 13 2013 16:26:18 GMT+0000 (UTC)"];
I've tried a few things for XXX such as z or Z but everything results in a nil date.
Having a date string with two separate timezone specifications is strange. It may work with:
EEE MMM dd yyyy HH:mm:ss zZ (z)
If the part in the parentheses will always be UTC then I would do:
EEE MMM dd yyyy HH:mm:ss zZ '(UTC)'
or maybe:
EEE MMM dd yyyy HH:mm:ss 'GMT'Z '(UTC)'

How to parse this format with NSDateFormatter

I want to parse the following string to date format with NSDateFormatter
Mon, 05 Aug 2013 00:00:00 +0000
What should I write as date format?
I've tried this but it did not work.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"Day, dd Mon yyyy hh:mm:ss a"];
[df setDateFormat:#"eee, dd MMM yyyy hh:mm:ss ZZZ"];
If this doesn't work, use this as a reference:
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

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