Why is NSDateFormatter returning nil? - objective-c

To be more precise this is not working for me:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
NSDate *prevday = [formatter dateFromString:#"2011-04-07T22:00:48Z"];
prevday is returning NIL.

You need to inserts ticks in the string:
#"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'"

According to this document;
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html%23//apple_ref/doc/uid/TP40002369-SW1
Date formatters use the following version of the standard;
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
so you'd use the date format 'yyyy-MM-ddTHH:mm:ss' I'm not sure about the Z, is that supposed to be the time zone? Z in the format string will give you the 3 letter time zone name.
Edited based on your edit above:
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
Is why it's returning nil.
The 'Z' expects the time zone to be in the 3 character time zone thing...
Drop the Z or escape it with a quote character. Read the tr35 doc above for more info.

Related

iOS: how to convert NSString to NSDate?

How would I convert this string recentUpdate to an NSdate? My recentUpdate outputs this 10/05/2014 06:43:PM and when a converted it to NSDate, it always results null.
Can someone please tell me what I am doing wrong? Any tips or suggestions are appreciated. My question is a little more specific compare to the other questions on stackoverflow so this isn't a duplicate question.
NSString *recentUpdate = sharedData.mUser.mRecentUpdateTime;
NSDate *date = [NSDate dateFromString:recentUpdate usingFormat:#"MM-dd-yyyy HH:mm:ss"];
Your format does not match the string you gave:
MM-dd-yyyy HH:mm:ss
10/05/2014 06:43:PM
^ ^ ^^
Here and here
If you are converting any string to NSDate, make sure your string format should be same as the format you are passing in date-formatter.
Example -
for string like 10/05/2014 06:43:PM, your date format should be - MM/dd/yyyy HH:mm:a
for string like 10-05-2014 06:43 PM, your date format should be - MM-dd-yyyy HH:mm a
otherwise result will be null .

How to check whats the format of a date on iOS?

In my application i get from a server some dates.
Some of the dates are in this format : "MMM dd, yyyy hh:mm:ss a" and some others in this : "yyyy-MM-dd HH:mm:ss Z" . I get the dates as strings from the server in one of the above formats. However i want all my dates to be in the first format. So how can i compare if the date is on the second format so i can change it? I know how to change formats , i just dont know how to compare a date with a specific format.
Thanks!
If you know the format that any given returned string is in then you could explicity set the format for each iteration of getting a date and then convert it to whatever format you need. Something like this example;
NSString *myString1 = #"01-30-2012";
NSString *myString2 = #"30-01-2012";
// Now convert the string to a date object
NSDateFormatter *myDateFormat = [[NSDateFormatter alloc] init];
[myDateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date1 = [myDateFormat dateFromString:myString1];
[myDateFormat setDateFormat:#"MM-dd-yyyy"];
NSDate *date2 = [myDateFormat dateFromString:myString2];
[myDateFormat release];
If those are the only formats, just check something simple like if the fifth character is a dash then it's second, otherwise first.

How can I get the localized date units?

I need a array with date units, for example Year,Month,Day....
I want them to be localized, for example in german it would be Jahr, Monat, Tag ...
It seems I can only get the localized string from date but not the units.
Is there a way to get those units from current calendar directly instead of translating them by hand?
As far as I know, you will need to provide your own translations for those units. You can get the names of the days and months with NSDateFormatter but not the units themselves. How the unit is translated depends on grammatical case and number, so the translation is context sensitive. Don't forget, some languages have grammatical numbers other than just singular and plural!
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CDT"]];
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
[timeZoneComps setHour:16];
//specify whatever day, month, and year is appropriate
NSDate *date=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *monthName = [[df monthSymbols] objectAtIndex:(mymonth-1)];
[df setDateFormat:#"EEE"];
NSString *weekName = [ df stringFromDate:date];
NSLog(#"The Week Name :: %#",weekName);
[df release];
You can use above code and particularly timeZoneWithAbbreviation:#"CDT" represents the abbreviation of various timezones like CDT, GMT,UTC,CST. I think it will be helpful to you.

How to find the date format of a string

Is there a way (an elegant way that is) to extract a date format from a string containing a date so that it may be converted into an NSDate via NSDateFormatter?
i.e
string = #"2011-1-10";
format = [extractor extractFormat:string];// format would = yyyy-dd-mm
[formatter setDateFormatter:format];
NSDate * date = [formatter dateFromString:string];
If that is the only information you have then it is impossible. For example, "10-10-10".

Format string for NSDateFormatter to produce milliseconds

Since my date is well formatted now all there is left for me to do is to print milliseconds too.
I already tried setDateFormat yyyy/mm/dd hh:mm:ss:ms
That didn't work. Any advice?
Try to use 'SS' specifier (with number of S's equal to number of digits you want to get - 3 in your case), e.g.
[formatter setDateFormat:#"yyyy/MM/dd hh:mm:ss:SSS"];
Try this format:
"yyyy/MM/dd HH:mm:ss.SSS"