Resolved Time difference between dates in a 'time format'

Discussion in 'iOS Programming' started by DennisBlah, Dec 21, 2014.

  1. DennisBlah, Dec 21, 2014
    Last edited: Dec 21, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I'm have the following info in my database:
    date,
    timefrom,
    timetill

    (I'm using timefrom and timetill, to make it easier with showing these info)

    Now I calculate the difference between these times like this:

    Code:
    NSString *start = [[agendaItems objectAtIndex:indexPath.row] objectAtIndex:indexOfStarttime];
        NSString *end = [[agendaItems objectAtIndex:indexPath.row] objectAtIndex:indexOfEndtime];
        
        NSString *date = [[agendaItems objectAtIndex:indexPath.row] objectAtIndex:indexOfDate];
      
        NSString *startDate = [NSString stringWithFormat: @"%@ %@", date, start];
        NSDateFormatter* fmt1 = [NSDateFormatter new];
        [fmt1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSString *eindDatum = [NSString stringWithFormat: @"%@ %@", date, end];
        NSDateFormatter* fmt2 = [NSDateFormatter new];
        [fmt2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate *date1 = [fmt1 dateFromString: startDatum];
        NSDate *date2 = [fmt1 dateFromString: eindDatum];
        NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
        double diffInHours = distanceBetweenDates / 3600;
    //p.s. I should be doing it other way around so I dont need to use fabs(diffInHours) I know ;-)
    
    Now I have a difference like: 3.500 hours.
    Now I want it to be: 3:30 hours

    Whats the easiest way to accomplish this?

    I can start storing the value as string, explode it on .
    if the 2nd value is bigger than 0 then, I can devide 60 by this number and glue it back together and seperate it by a :

    There must be an easier way! :(
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Thanks for the push in the right direction Chown!

    Code:
    NSString *start = [[agendaItems objectAtIndex:indexPath.row] objectAtIndex:indexOfStarttime];
        NSString *end = [[agendaItems objectAtIndex:indexPath.row] objectAtIndex:indexOfEndtime];
        
        NSString *date = [[agendaItems objectAtIndex:indexPath.row] objectAtIndex:indexOfDate];
      
        NSString *startDate = [NSString stringWithFormat: @"%@ %@", date, start];
        NSDateFormatter* fmt1 = [NSDateFormatter new];
        [fmt1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSString *eindDatum = [NSString stringWithFormat: @"%@ %@", date, end];
        NSDateFormatter* fmt2 = [NSDateFormatter new];
        [fmt2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    
    
    //Difference in 'time format' here
        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDateComponents *components = [cal components:NSMinuteCalendarUnit fromDate:date2 toDate:date1 options:0];
        int diffMinutes = abs((int)components.minute);
        int diffHours = diffMinutes / 60;
        diffMinutes -= (diffHours * 60);
    
        NSString *diffMin = [NSString stringWithFormat: @"%i", diffMinutes];
        if(diffMin.length == 1)
            diffMin = [NSString stringWithFormat: @"0%@", diffMin];
        NSString *theDiff = [NSString stringWithFormat: @"%i:%@", diffHours, diffMin];
    
    If this can be done easier, please tell me :)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Store your dates as NSDates and not NSStrings, is what I would suggest.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    What is the purpose of the code in red:
    Code:
        NSString *startDate = [NSString stringWithFormat: @"%@ %@", date, start];
    [COLOR="Red"]    NSDateFormatter* fmt1 = [NSDateFormatter new];
        [fmt1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [/COLOR]    
        NSString *eindDatum = [NSString stringWithFormat: @"%@ %@", date, end];
    [COLOR="red"]    NSDateFormatter* fmt2 = [NSDateFormatter new];
        [fmt2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [/COLOR]
    You create two NSDateFormatters then don't use them anywhere in your most recently posted code. Either you're not showing all your code, or you're pointlessly creating NSDateFormatters.

    If you're not showing all your code, then how do you expect us to know what it does, and tell you how to improve it?

    If the NSDateFormatters are used, then why is it necessary to create two identical ones? Even in your first post, fmt2 is unused after being created.


    Code:
        NSString *diffMin = [NSString stringWithFormat: @"%i", diffMinutes];
        if(diffMin.length == 1)
            diffMin = [NSString stringWithFormat: @"0%@", diffMin];
        NSString *theDiff = [NSString stringWithFormat: @"%i:%@", diffHours, diffMin];
    
    You should look at the details of how formatting specifiers works. Your conditional and reformat can be replaced by the single formatting string @"%i:%02i".
     
  6. DennisBlah, Dec 22, 2014
    Last edited: Dec 22, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #6
    Sorry all,

    i tried to change my variable names to more understandable english :p
    Still missed half of it :-$

    this is what I got:
    Code:
        NSString *startDate = [NSString stringWithFormat: @"%@ %@", tDate, startTime];
        NSString *endDate = [NSString stringWithFormat: @"%@ %@", tDate, endTime];
    
        NSDateFormatter* fmt1 = [NSDateFormatter new];
        [fmt1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate *date1 = [fmt1 dateFromString: startDate];
        NSDate *date2 = [fmt1 dateFromString: endDate];
    
        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDateComponents *components = [cal components:NSMinuteCalendarUnit fromDate:date2 toDate:date1 options:0];
    
        int diffMinutes = abs((int)components.minute);
        int diffHours = diffMinutes / 60;
        diffMinutes -= (diffHours * 60);
    
        NSString *theDiff = [NSString stringWithFormat: @"%i:%02i", diffHours, diffMinutes];
    
    There I actually use the dateformatter. Right now I removed the dateformatter 2.
    My dates come from my sqlite database as yyyy-mm-dd (DATE field)

    Wow thanks for pointing me this out!
    Excuse me for my lack of knowledge. I learned obj C in xCode from tutorials.
    I don't know very well where to find this kind of things out.


    My information comes from sqlite database, and I need to format the value to NSDate, and that is actually what I'm doing here right?
    Code:
        NSString *startDate = [NSString stringWithFormat: @"%@ %@", tDate, startTime];
    
        NSDateFormatter* fmt1 = [NSDateFormatter new];
        [fmt1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate *date1 = [fmt1 dateFromString: startDate];
    
    Or can this be simplyfied?
     
  7. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #7
    Code:
    int diffMinutes = [B]abs[/B][B]([/B](int)components.minute[B])[/B]; 
    Is this intended? I can see a few problems down the road
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Apple has tons of programming guides. Many of them are linked to from the API reference. They're a great place to learn more. Do you know about Quick Look in Xcode?
     
  9. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #9
    Had to introduce a collegeue of mine into xCode.
    Also she mentioned something about xCode giving 'comments' about functions ?
    I still haven't seen it xD I'll ask about this one, I think it will be very usefull.

    ----------

    Sorry JohnsonK ? What do you mean. The reason I do this, is to make sure that my result in difference will alwasy be absolute (positive).

    This is a part for an application Im building for a drivingschool.
    It shows icons on the days where the current loggedin student has a lesson scheduled, when pressed, it will fill the tableview below the calendar with start time end time and difference in 00:00 format, and more information, like start location and instructor.

    Right now I'm working my way up to show all lessons for current logged in 'instructor' and ALL lessons for current logged in 'company owner'.
     

Share This Page