The following is the code I am using to change the date from one format to another. In the following code child.dob is in NSDate as yyyy-MM-dd format I want to change to MMM dd, yyyy format I am able to change the date format however I am getting a warning message here . Please help me to convert date without warning.. Thanks in advance.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:child.dob];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
dobLabel.text = [dateFormatter stringFromDate:date];
This is the warning message I am getting:
:801: warning: incompatible Objective-C types ‘struct NSDate *’, expected ‘struct NSString *’ when passing argument 1 of ‘dateFromString:
Yep, you use NSDateFormatter dateFromString to convert an NSDate object to a displayable string. Use NSDateFormatter stringFromDate to go the other way.
If you use NSLog on an NSDate then under the covers it does a
descriptioncall which presents a fixed format. However,descriptionshould not be used for “real” work since the format presented is not rigorously defined and could change at any time.Note that there is a “feature” of NSDateFormatter in that it can erroneously (in my mind) add or remove an AM/PM value based on phone settings, even when you have explicitly specified the format to NSDateFormatter.