-5

I am working on Quiz App. In my app I want to get difference between quiz start time and end time. How to get difference between start and end time.

Thanks in advance.

Girish
  • 4,692
  • 4
  • 35
  • 55
Alex
  • 403
  • 8
  • 13

3 Answers3

1

You can use NSDate - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate for this purpose.

Dmitry Zhukov
  • 1,809
  • 2
  • 27
  • 35
0

Use this

 NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"MM/dd/yy"];
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yy"];
    NSString *dateString11 = [dateFormat stringFromDate:today];
    NSDate *today1 = [dateFormatter1 dateFromString:dateString11];
    NSDate *ExpDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"ExpDate"];
    NSTimeInterval interval = [ExpDate timeIntervalSinceDate:today1];
    int diff=interval/86400;

i hope this code useful for you.

And Another code for you.

 NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDate *ServerDate = [dateFormatter1 dateFromString:@"04/26/2013"];
[dateFormatter1 release];
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate];
int diff=interval/86400;
NSLog(@"%d",diff);
D.M Patel
  • 145
  • 1
  • 7
-2

The NSDate class has a method timeIntervalSinceDate that does the trick.

NSTimeInterval interval = [firstDate timeIntervalSinceDate:secondDate];
Girish
  • 4,692
  • 4
  • 35
  • 55