Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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! :(
 
Last edited:
Look at NSDateComponents.

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 :)
 
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".
 
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)

What is the purpose of the code in red:
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".

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.


Store your dates as NSDates and not NSStrings, is what I would suggest.
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?
 
Last edited:
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
 
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?

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.

----------

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

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'.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.