Parse string as date contains words - ios7

I want to parse "DATE: 12-9-2014 TIME: 20-13-59" this string to NSDate. I have tried it parsing it using format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"DATE: dd-MM-yyyy TIME: HH-mm-ss"];
NSDate *date = [dateFormatter dateFromString:timeStamp];
NSLog(#"ASD: %#", date);
It returns null. What i am doing wrong, please advice.
Thanks

Add quotes around the parts of the date string that are literal, and are not formatting characters:
[dateFormatter setDateFormat:#"'DATE:' dd-MM-yyyy 'TIME:' HH-mm-ss"];

Related

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

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

NSDateFormatter returns nil

I'm trying to parse a date passed in the format:
"2014-03-26T05:07:42.14286Z"
My NSDateFormatter code looks like this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'SS'Z'"];
self.createdAt = [dateFormatter dateFromString:#""2014-03-26T05:07:42.14286Z""];
But it just returns nil. I've tried multiple variations with and without the ticks but I seem to be missing something. Am I using NSDateFormatter incorrectly, misunderstanding the usage of ticks or something else entirely?
The formatter returns nil if the given string doesn't correspond to the expected format. Your format string was almost right, you just needed to :
Apostrophe should only be used for literals
Milliseconds should be specified as 'SSS' and match the separation (use ss.SSS)
The correct format string is :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *d = [df dateFromString:#"2014-03-26T05:07:42.14286Z"];
Just replace the below line to modified line
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'SS'Z'"];
Modified line:-
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];

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

Setting date format for particular string format in ios

I'm trying to set NSDateFormatter for the below string:
2012-12-18T09:05:24.000Z
It's the output of S3Object Summary lastmodifed object. What is the format I have to set it to if I need to change the display format?Any idea?
NSString *stringe = #"2010-06-21T20:06:36+00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *reqDate = [dateFormatter dateFromString:string];
NSLog(#"%#",[dateFormatter stringFromDate:reqDate]);
Now you will get the date in NSDate object so you can change that date to as per your requirement.
For 2012-12-18T09:05:24.000Z
The format string is
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

convert date from one format to another format

HI all,
i have a date string like this
2011-03-31 13:32:02
i want to convert this date string to display like this
Thursday – March 31, 2011
Please help to solve this
Thanks in advance
Use NSDateFormatter. First initialize the formatter with the appropriate format to parse the source string, something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Then use it to parse the date string:
NSDate *date = [formatter dateFromString:string];
Now you can create a new formatter to output in the new format, or just reuse the existing one:
[formatter setDateFormat:#"EEEE - MMMM dd, yyyy"];
NSString *output = [formatter stringFromDate:date];