开发者

problem with setDateFormat for my iPad app

开发者 https://www.devze.com 2023-02-19 17:22 出处:网络
I use the following to get the date. NSDate *today =[NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

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

精彩评论

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