I ve already tried with NSDate but with no luck. I want the difference between for example 14:10 and 18:30.
Hours and minutes.
I Hope you can help me shouldn t be that complicated :)
I ve already tried with NSDate but with no luck. I want the difference between for example 14:10 and 18:30.
Hours and minutes.
I Hope you can help me shouldn t be that complicated :)
There s no need to calculate this by hand, take a look at NSCalendar
. If you want to get the hours and minutes between two dates, use something like this:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
fromDate:firstDate
toDate:otherDate
options:0];
[gregorianCalendar release];
You now have the hours and minutes as NSDateComponents and can access them as NSIntegers
like [components hour]
and [components minute]
. This will also work for hours between days, leap years and other fun stuff.
Here s my quick solution:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"HH:mm"];
NSDate *date1 = [df dateFromString:@"14:10"];
NSDate *date2 = [df dateFromString:@"18:09"];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int hours = (int)interval / 3600; // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
NSString *timeDiff = [NSString stringWithFormat:@"%d:%02d", hours, minutes];
The NSDate class has a method timeIntervalSinceDate that does the trick.
NSTimeInterval secondsBetween = [firstDate timeIntervalSinceDate:secondDate];
NSTimeInterval is a double that represents the seconds between the two times.
NSString *duration = [self calculateDuration:oldTime secondDate:currentTime];
- (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
{
NSDate *date1 = oldTime;
NSDate *date2 = currentTime;
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int hh = secondsBetween / (60*60);
double rem = fmod(secondsBetween, (60*60));
int mm = rem / 60;
rem = fmod(rem, 60);
int ss = rem;
NSString *str = [NSString stringWithFormat:@"%02d:%02d:%02d",hh,mm,ss];
return str;
}
In one field I need to store not a datetime pair, i.e. a standard Oracle date. 01/10/2009 22:10:39 But time only 22:10:39 I think that save disk space (I have 2 million rows) or provide faster ...
I would like to know if there is a way that I can have the time be auto filled when the user enters 10 I then want it to show 10:00. how can I do this?
I m trying to take timestamps in this format: 2009-11-16T14:05:22-08:00 and make turn them into something like 2009-11-16 How do I remove everything after the "T"?
I m currently using the following code to format time on Google Android: DateFormat.getDateTimeInstance().format(millis) While this code honors my timezone and locale settings, it ignores the 24 ...
I am writing a program that will be used on a Solaris machine. I need a way of keeping track of how many seconds has passed since the start of the program. I m talking very simple here. For example I ...
I m rewriting an old program to do some new stuff, and suddenly I get a segmentation fault error on the following line of code: time_t seconds_since_time_begun = time(0); Why, oh why? Update: I ...
I have a dedicated server running Cent OS with a Parallel PLESK panel. I need to run a PHP script every second to update my database. These is no alternative way time-wise, it needs to be updated ...
I want to add time values, e.g. 2:00 + 3:00 = 5:00. How can I do that in ASP.NET using C#?