views:

37

answers:

1

Hi, everyone,

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.

+3  A: 

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

texmex5
@texmex5, thank you for your reply. But how to set the startDay and the endDay, I don't know how to set the date.
Questions
I updated my answer to comply with your comments.
texmex5
@texmex5, thank you for your reply. It is very useful.
Questions