Objective C dateformatter how do i format this - nsdateformatter

I get the following date from an API
"created_at":"Wed Jun 19 20:36:48 +0000 2013"
I don't know how to handle the "Wed" and "Jun", can't find any good documentation for this.
[dateFormatter setDateFormat:#"? ? dd HH:mm:ss '+00:00' YYYY"];

Related

Why I can't get time this way

strTime=#"13:00";
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"hh:mm"];
NSDate * date = [df dateFromString:strTime];
Everything seems fine.
Yet the content of date is nil.
Why?
What did I do wrong?
Also I changed to #"HH:mm"
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"HH:mm"];
NSDate * date = [df dateFromString:strTime];
Result is the following:
(lldb) po strTime
13:00
(lldb) po date
2000-01-01 06:00:00 +0000
(lldb) po strTime
15:45
(lldb) po date
2000-01-01 08:45:00 +0000
I think I want something closer to today's date
I suppose what you did wrong is not read the documentation enough. But it's actually pretty obscure. The Data Formatting Guide points you to ยง8 Date Format Pattern, in Unicode Technical Standard #35, which says this about h:
Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match a 24-hour-cycle format (H or k). Use hh for zero padding.
And says this about H:
Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a 12-hour-cycle format (h or K). Use HH for zero padding.
So you should try using HH:mm as your format:
[df setDateFormat:#"HH:mm"];

Using NSDateFormatter on iOS

I am currently using an NSDateFormatter on my application to show the date in the format that i want. My NSDateFormatter looks like this : [formatter setDateFormat:#"MMM dd, yyyy hh:mm"]; which would give me dates like : Sep 02 , 2012 08:30 .
I have 2 questions. i want to show the time in military time how can i do that with the formatter? I know how to show the AM/PM but i would like the time above to look 20:30. Secondly how can i make the month appear as a number? September would be 09 .
This site has a nice table with the format specifiers you can use, follow up from that.
In this case, you should use [formatter setDateFormat:#"MM dd, yyyy HH:mm"]; To get 09 02, 2012 20:30
Use capital H for the hour and only two M's for the month.
[formatter setDateFormat:#"MM dd, yyyy HH:mm"];

How to convert a string to a valid DateTime

Need to convert this string:
Mon Oct 31 16:18:15 CDT 2011
Into a valid DateTime value.
Have tried every variation of the date time styles with DateTime.Parseto no avail.
Any ideas?
The problem is with the CDT you have there. This is not a valid portion of a string representing a DateTime.
You may have luck with replacing this with a valid representation of a timezone -0500 and the K format specifier for it.
You can use the following format string to parse the string:
ddd MMM dd HH:mm:ss CDT yyyy
For instance:
DateTime.ParseExact("Mon Oct 31 16:18:15 CDT 2011",
"ddd MMM dd HH:mm:ss CDT yyyy",
CultureInfo.InvariantCulture);
I suggest reading the documentation for Custom Date and Time Format Strings on MSDN.

How to convert NSString to NSDate?

I want to search my Array of Dictionary for particular date.Ex I want to search my array of dictionary for date "16 Jan 2012" which is in string format but my dictionary item in array contains date and time, say "16 Jan 2012 somehours:somemins:somesecs".I am converting string format date in NSDate format but I am getting date as 2012-01-15 18:30:00 +0000 instead of 2012-01-16.I am using NSPredicate to search for the date which convert date into seconds as follows "Date == CAST(348345000.000000, "NSDate")" and compare so even though my records contain date as "16 Jan 2012 somehours:somemins:somesecs" it will not satisfied the criteria.I want that the records/array containing date as "16 Jan 2012 somehours:somemins:somesecs" should satisfied the search criteria.Please can anyone know how to achieve this?
Thanks in advance!
To rid yourself of NSDateFormatter's automatic adjustment for your time zone use something like this.
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
To elaborate; "16 Jan 2012" Contains no hour, minute, or seconds value, so when parsing it with an NSDateFormatter one would expect an NSDate with a description of "2012-01-16 00:00:00 +000" But that's not what you're getting because the date formatter is adjusting for the 5:30 Hours between GMT and India?(Assuming based on :30 differential). By setting the date formatter's time zone explicitly you avoid this problem.
You need to use NSDateFormatters to convert the date string from one format to another. You can have an inputDateFormatter where you use dateFromString to give you an NSDate, and then feed this into an outputDateFormatter which gives your desired string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateInFormatYouWant = [dateFormatter stringFromDate:yourDate]
I'm not sure if there is a more efficient way of doing it than this.

NSDate formatter

I try to convert a string to NSDATE with no luck unfortunately.
Friday, October 22, 11:26:45 ET 2010
I know the options for formatting (http://sree.cc/objective-c/nsdate-format-string-in-objective-c) but i cant get it to work.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???????"];
anyone?
Date Format Specifiers.
So you'd probably need something like:
[dateFormatter setDateFormat:#"eeee, MMMM dd, HH:mm:ss z yyyy"];
eeee - Local day of week spelled out
MMMM - Month spelled out
dd - day of month with no leading zeros
HH - hour of day (24 hour format)
mm - minutes of hour (with leading zero)
ss - seconds of minute (with leading zero)
z - timezone (short wall time)
yyyy - calendar year