problem with setDateFormat for my iPad app
I use the following to get the date.
NSDate *today =[NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//manage day of week and date.
[formatter setDateFormat:@"EEE"];
dayOfWeek = [formatter stringFromDate:today];
[formatter setDateFormat:@"MMM"];
theDate = [formatter stringFro开发者_开发知识库mDate:today];
// manage the time and am/pm
[formatter setDateFormat:@"h:mm"];
theTime = [formatter stringFromDate:today];
[formatter setDateFormat:@"a"];
amPM = [[formatter stringFromDate:today] retain];
[formatter release];
My app crashes if I use [formatter setDateFormat:@"d MMM"]
. Do I need to convert the integer to string ? Is so please correct my code on how to get the current date
This should do the trick:
[formatter setDateFormat:@"d MMM"];
NSString *theDate = [formatter stringFromDate:today];
setDateFormat doesn't return anything, stringFromDate returns what you are looking for.
And don't retain like you did in this line:
amPM = [[formatter stringFromDate:today] retain];
The formatter has already a retain count of 1 by using alloc.
精彩评论