Hi I have a question regarding NSDateFormatter. Maybe Im missing something but my NSDateFormatter is not adjusting the date correctly only the time:
// NSDateFormatter (self.df) initialized in another method.
[self.df setDateFormat:@"MMM dd, YYYY hh:mm a"];
NSString *dpDate = [(UITextField*)[dateCell viewWithTag:101] text];
NSLog(@"tf date: %@",dpDate);
NSDate *date = [[NSDate alloc] init];
date = [self.df dateFromString:dpDate];
NSLog(@"Date: %@",[self.df stringFromDate:date]);
However after printing it seems the date doesn't format correctly Below are a few outputs:
Consult Output 1:
xxx[5262:60b] tf date: Dec 05, 2013 09:35 AM
2013-12-05 09:35:17.718 xxx[5262:60b] Date: Dec 23, 2012 09:35 AM
Consult Output 2:
xxx[5262:60b] tf date: Jan 16, 2014 09:45 AM
2013-12-05 09:36:21.151 xxx[5262:60b] Date: Dec 22, 2013 09:45 AM
In addition, I am only setting the dateFormat once.
You need to use
yyyy
, notYYYY
. The first is the regular year number (with respect to the configured calendar) and the second is for week-based calendar year.See this detailed article on how to use NSDateFormatter, and the distinction between the two.