NSDateFormatter returns nil with format YYYY-MM-ddTHH:mm:ssZ - objective-c

I'm trying to get the NSDate from a string with the following format 'YYYY-MM-ddTHH:mm:ssZ' using the NSDateFormatter. The NSDateFormatter returns always nil. Here is how I tried to do that:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *dateFromString = [dateFormatter dateFromString:#"2013-08-09T18:30:00+02:00"];

that would be a better formatter.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];

Your date format string should be as follows:
#"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"
Note the lower cased 'yyyy'. The uppercase Y means "Week of Year" based calendar.
See this answer: Difference between 'YYYY' and 'yyyy' in NSDateFormatter and the Unicode standard for more info: http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns

Related

How do I get the alphanumeric abbreviated day of month in objective-c?

NSDate in Objective-c used to have dateWithNaturalLanguageString which accepted the use of abbreviated alphanumeric days of month with in strings like: #"Aug 2nd, 2010", but this method is deprecated, and I am trying to use NSDateFormatter instead:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy"];
but I can not use the following string with the above format:
NSDate date* = [dateFormatter dateFromString:#"Aug 2nd, 2010"];
since it will cause the date to be null due to incompatible format. I checked out the unicode standard date formats but I could not find anything that has an abbreviated alphanumeric day of month, and I am forced to use #"Aug 02, 2010" instead. But this is not desirable since I need abbreviated alphanumeric day of month both for setting a date from a string and getting a string from a date. After searching hours through various documentations I am out of ideas. Is there anyway other than the deprecated dateWithNaturalLanguageString? Or do I have to make a method of my own?
NSDateFormatter does not support ordinal suffixes (in English).
An alternative is to remove the suffix with Regular Expression
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.dateFormat = #"MMM dd, yyyy";
NSString *dateString = #"Aug 2nd, 2010";
NSString *trimmedString = [dateString stringByReplacingOccurrencesOfString:#"(\\d{1,2})(st|nd|rd|th)"
withString:#"$1"
options: NSRegularExpressionSearch
range:NSMakeRange(0, dateString.length)];
NSDate *date = [dateFormatter dateFromString:trimmedString];
NSLog(#"%#", date);

NSDateFormatter: Converting "5/13/2012 6:05am" (US, EDT) to "13/05/2012 12:05" (IT, GMT+1)

I am retrieving a time value from a server. The format is:
"5/13/2012 6:05am"
It is not specified in the string but I know it is "EDT".
I need to:
1) Get rid of the am/pm thing.
2) Convert it from EDT (or other timezone) to the local GMT+/-x time.
For my timeZone (Italy, GMT+1) it should become:
"13/05/2012 12:05"
How can I do this with NSDateFormatter?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm"];
...?
Thanks
Nicola
First of all, you set your NSDateFormatters dateFormat to the source format. In your case:
[dateFormatter setDateFormat:#"M/d/yyyy h:mma"];
NSDate *myDate = [dateFormatter dateFromString:#"5/13/2012 6:05am"];
This creates a new instance of NSDate. Now you set the formatters dateFormat to your target format:
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm"];
NSString *myString = [dateFormatter stringFromDate:myDate];
You could also use two date formatters, one for converting you'r string into NSDate instance and one to convert it back to the corrected NSString.
For further informations and the complete list of the current format specs refer to the unicode reference and the Apple Docs

Create NSDate timezone issue

I am loading in dates from my web service, I'm sending dates in the format (GMT times): 02/11/11 10:56:09
I am creating an NSDate form this using NSDateFormatter as such:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
This works great, after I'm comparing this to the current date to get relative time intervals.
The problem is when the phone is set up in a different timezone, when I load in the date from my api, and use the date formatter, what seems to be happening is the phone is assuming the date string is local time and it's converting it to GMT.
Example:
I load in a date with the time 10am from the api.
The phone is set to PDT.
The date formatter is creating an NSDate assuming that my date string with 10am, is actually relevant to the phone.
I end up with a date and time equal to 5pm, adding 10 hours.
I am trying to specify in my date formatter that the string is GMT, but I'm having trouble, I've tried the following, adding GMT to the format:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss GMT"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
This is not working.
Can anyone give any advice ?
Solution
Just a recap, I got it working with a terrible work around by appending GMT to the original string, and formatting that:
NSString * cheat = [NSString stringWithFormat:#"%# GMT", str];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss zzzz"];
NSDate *journeyDate = [dateFormatter dateFromString:cheat];
[dateFormatter release];
return journeyDate;
This was a kind of unstable hack, because if the string changed to include a timezone, it wouldn't work anymore. For anyone who needs to do as myself, the following is just a quick example on how to create an NSTimeZone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
return journeyDate;
Thanks for the quick help.
I suspect you just want to use NSDateFormatter.setTimeZone to force it to use UTC. You don't want to change the format string because presumably the string doesn't include the letters "GMT" - instead, you want to change which time zone the string is interpreted in, which is what setTimeZone will do.
You should use the setTimeZone method: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

Issues with String to NSDate

My dateFromString is not working and i'm not sure why
NSString *purchase = #"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];
date is 'invalid CFStringRef'
See anything i might be overlooking?
Try changing the hours in the formatter to capitals, i.e. yyyy-MM-dd HH:mm:ss
I'm not sure if that will solve your error, but that is the correct way to parse a 24 hour time
in the format
"hh" means Hour [1-12].
"HH" means Hour [0-23].
See UTS Date Field Symbol Table for the date format specifiers.
Try this:
NSString *purchase = #"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];
Date Formatters Guide - HH is used for 24 hour times, hh for 12 hour.
If you mean that "date" is not accepted as an NSString by another function, that's because it's not a string, it's an NSDate.
(Where, precisely, are you getting the error message, and what is the full text?)

"setDateFormat" does not work with GMT?

i'm wondering why my setDateFormat does not work when i use it with a different timezone , here GMT :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"dd-MM-YYYY"]; //this doesn't work, nothing appears
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; //this works
but if i use the default timeZone, it works :
NSDateFormatter *Dateformat = [[NSDateFormatter alloc]init];
[Dateformat setDateFormat:#"dd-MM-YYYY HH:mm:ss"]; //this works
Thanks for your help
Paul
Have you tried to NSLog a string by using this dateFormatter, its woking for me..when i make a string as-
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateStr);
and its give me this -
09-08-2011
Additionaly, you should probably be using: [dateFormatter setDateFormat:#"dd-MM-yyyy"] instead of [dateFormatter setDateFormat:#"dd-MM-YYYY"].
Using YYYY is a common mistake according to the apple documentation. It returns the year number of the year the week is in (according to ISO week numbering scheme). This can be off one year. Lowercase yyyy is normally the correct version.
See: iphone Get current year as string
(The comment of Anna Karenina)