Questions
Questions

Reputation: 20875

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.

Upvotes: 1

Views: 2392

Answers (1)

texmex5
texmex5

Reputation: 4344

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

Upvotes: 6

Related Questions