开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜