开发者

How to set the NSDate on iPhone?

I want to ask 2 questions about the NSDate of iPhone application.

1) How to set the NSDate *startDay to 01-01-2010 and NSDate *endDay to 31-12-2010

2) how many day between the startDay and the endDay.

What should I do? Thank you very much开发者_运维问答.


For handling dates with different formats you would need to use NSDateFormatter

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"mm-dd-yyyy"];

To get date with specified format

NSString *dateString = [format stringFromDate:date]; 

To create a date from string with specified format:

NSDate *date = [format dateFromString:dateString];

NSDateFormatter documentation

To find the difference between two dates:

NSTimeInterval interval = [endDay timeIntervalSinceDate:startDay];

timeIntervalSinceDate documentation

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜