NSDateFormatter gets stringFromDate wrong - objective-c

I have a string as follows:
2012-11-01
And I want to get a string representing the day from that. So this is how I approach it:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-mm-dd"];
NSDate *date = [dateFormatter dateFromString: #"2012-11-01"];
[dateFormatter setDateFormat: #"EEE"];
NSString *string = [dateFormatter stringFromDate: date];
NSLog(#"%#", string);
And the output is:
Sun
When it really should be:
Thu
(If my calculations are correct!)
So what is the problem here? Is this an internal bug? Something wrong with my code?
Thanks!

Change your first date format string to:
[dateFormatter setDateFormat: #"yyyy-MM-dd"]; // Capital MM for month
It should work then.

Related

NSDateFormetter losing an year

I was using NSDateFormatter to convert a date NSString instance to a NSDate object. At a point, I realized that something isn't right, when I did a check, it seemed like NSDateFormatter was loosing a whole year when converting from a string to a NSDate.
The code looks like this:
NSString *dateString = [dictionary objectForKey:#"match_date_time"];
if (dateString) {
// dateString is "2012-10-30 04:30:00"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
self.date = [formatter dateFromString:dateString];
// self.date ends up 2011-10-30 04:30:00 +0000, yes 2011 not 2012.
}
Does anyone know why does NSDateFormatter fails so miserably?
Thanks!
just write below code
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];

How can I parse this string to NSDate with NSDateFormatter?

I have a date as a string in the format:
2010-12-31 20:21:00 +0200
What I'd like to do is parse this using NSDateFormatter to an NSDate object but I'm having difficulty matching the format properly.
NSDateFormatter *dateFormatter = NSDateFormatter.alloc.init;
[dateFormatter setTimeZone: NSTimeZone.localTimeZone];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss'Z'"];
[dateFormatter dateFromString: #"2010-12-31 20:21:00 +0200"] // returns nil ;
Can anybody help me find the correct format? thanks
This code works:
NSDateFormatter *dateFormatter = NSDateFormatter.alloc.init;
[dateFormatter setTimeZone:NSTimeZone.localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:#"2010-12-31 20:21:00 +0200"];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
And I just want you to know, kind sir, that my eyes bleed when I see this NSDateFormatter.alloc.init
NSDateFormatter* format = [[NSDateFormatter alloc]init];
[format setDateFormat:#"MM/dd/YYYY"];
NSString * currentDate = [format stringFromDate:[NSDate date]];
NSDate *date = [format dateFromString:currentDate];

NSDateFormatter and /'s

I have a NSString that I want to load into a NSDate. The string looks like: 21/10/2011 5:42:02 PM
What I have written is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
The date ends up with a null value.
There is obviously something wrong with the date formatter but I am not sure what.
Any help would be greatly appreciated.
If the string doesn't match the format, then it will return null.
For example, here's an example that matches the format and one that doesn't:
NSString *dateString = #"11/09/2011 12:22:02";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"dateString; %#", date);
NSString *dateString2 = #"2011-11-09 12:22:02 +0000";
NSDate *date2 = [dateFormat2 dateFromString:dateString2];
NSLog(#"dateString; %#", date2);
This outputs:
2011-11-09 07:27:54.342 Craplet[45608:707] dateString; 2011-09-11 05:22:02 +0000
2011-11-09 07:27:54.343 Craplet[45608:707] dateString; (null)

How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C?

I have read date from XML which give me back string where the date is like DD-MM-YYYY. But when I want to add it to my core data database, SQLite sorts me that wrong so I have to convert it to date format or to string like: YYYY-MM-DD. But SQLite does not support either Date() or To_Date() functions. Can you help me?
You can use NSDateFormatter to format a date in Objective C.
Here's a quick example which might not do exactly what you want, but should hopefully give some pointers:
// Convert my dd-mm-yyyyy string into a date object
NSString *stringDate = #"01-02-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
Hope this helps!
Nick.
Using Nick's tutorial and answer the solution is :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSDate *date = [dateFormatter dateFromString:#"01.02.1989"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
// Convert my date object into yyyy-mm-dd string object
[dateFormatter1 setDateFormat:#"yyyy-MM-dd"];
NSString *formattedStringDate = [dateFormatter1 stringFromDate:date];
[dateFormatter release];
[dateFormatter1 release];
NSLog(formattedStringDate);
objektObrat.dateOfObrat = [[NSString alloc] initWithString:formattedStringDate];

NSDate formatter Problem its getting null while converting NSString to NSDate

hi to all
I am Struggling to convert NSString to NSDate with the Help of formatter but its going to be null
Can any one help please
here is my code...
NSString *dateString=[NSString stringWithFormat:#"10/26/2010/09:56:56 PM"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"mm/dd/yyyy HH:mm:ss a"];
NSDate *localDateTime =[dateFormat dateFromString:dateString];
NSLog(#"client date is %#",localDateTime);
Thx in Advance
Could be because your dateString and format does not match.
10/26/2010/09:56:56 PM
mm/dd/yyyy HH:mm:ss a
Do you see the difference? :) They actually have to match. In the format you have a space between the year and hours and in the actual date there is a slash. Month you match by 'mm' which is actually minutes and should be 'MM'. Change to:
10/26/2010 09:56:56 PM
MM/dd/yyyy HH:mm:ss a
There might be more problems so I suggest you actually look at the guide to get it right.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
NSDate *formatterDate = [dateFormatter dateFromString:#"1999-07-11 at 10:30"];
[dateFormatter release];
Try this out.