1

I have a String with the time as follows;

NSString *time = "06.45 pm";

I need to assume that this time is on today's date and then get the current System time, and calculate the time difference between the two.

How can i do this ?

Note: The above time doesn't specify a date, (Only the time is given)

lnafziger
  • 25,760
  • 8
  • 60
  • 101
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140
  • The current system time is an exact point in time -- it includes seconds, a day, a month, and a year. Do you want that string to represent 6:45 PM on whatever date you happen to do the math, or what? – jscs May 30 '12 at 01:10
  • 6.45 pm should be todays date. So to calculate the time difference from the current time to 6.45 pm. I need to use the `timeIntervalSinceDate:` but i am not sure how to get the time difference . – Sharon Watinsan May 30 '12 at 01:12
  • how do you know that `time` you given is the same date as current system time. – HelmiB May 30 '12 at 01:13
  • take a look this : http://stackoverflow.com/questions/1792512/get-time-between-two-times-of-the-day – HelmiB May 30 '12 at 01:13

2 Answers2

3
NSString *time = @"06.45 pm";

NSDate *date1;
NSDate *date2;
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh.mm a"];
    date1 = [formatter dateFromString:time];
    date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
    [formatter release];
}
NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
int hour = interval / 3600;
int minute = (int)interval % 3600 / 60;

NSLog(@"%@ %dh %dm", interval<0?@"-":@"+", ABS(hour), ABS(minute));
Joey
  • 2,912
  • 2
  • 27
  • 32
  • Why are you calculating and formatting the result by hand? Use `timeIntervalSinceDate:` and another date formatter. – jscs May 30 '12 at 01:18
  • Yes, your solution works. but how can i use `timeIntervalSinceDate ` in the example you have given ? – Sharon Watinsan May 30 '12 at 01:21
2

Make sure that when you do this that you are using calendar operations so that it takes into account time changes (daylight savings time, leap seconds, etc.):

NSString    *time  = @"06.45 pm";
NSCalendar  *cal   = [NSCalendar currentCalendar];
NSDate      *today = [NSDate date];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh.mm a"];

// Convert time to a NSDate and break it down into hours, minutes, and seconds:
NSDate           *timeAsDate = [formatter dateFromString:time];
NSDateComponents *timeComps  = [cal components:NSHourCalendarUnit |
                                               NSMinuteCalendarUnit |
                                               NSSecondCalendarUnit
                                      fromDate:timeAsDate];

// Take today's date and add "time" to it:
NSDateComponents *comps  = [cal components:NSYearCalendarUnit |
                                           NSMonthCalendarUnit |
                                           NSDayCalendarUnit
                                  fromDate:today];
comps.hour               = timeComps.hour;
comps.minute             = timeComps.minute;
comps.second             = timeComps.second;
NSDate *timeOnTodaysDate = [cal dateFromComponents:comps];

// Now calculate the difference:
NSTimeInterval difference = [today timeIntervalSinceDate:timeOnTodaysDate];
lnafziger
  • 25,760
  • 8
  • 60
  • 101