开发者

How to set the NSDate on iPhone?

开发者 https://www.devze.com 2023-01-11 08:57 出处:网络
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

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

精彩评论

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