Objective-C midpoint between two times - iphone

I have two NSDates formatted as "h:mm a" as time, (i.e. 6:00 AM and 8:00 PM).
I am trying to find out what time is the midpoint between those two times.
For the example above, the midpoint between 6:00 AM and 8:00 PM would be 1:00 PM.
But I do not know how to do this in objective-C for the iPhone sdk.
Any help or suggestions or code would be greatly appreciated.

Perhaps something like the following is what you're after?
// Assuming you have two NSDate instances: start and end.
NSTimeInterval difference = [end timeIntervalSinceDate:start];
NSDate *middle = [NSDate dateWithTimeInterval:difference / 2 sinceDate:start];
See the NSDate Class Reference for more information.

Convert the times to a time interval, average the two numbers, then convert it back to an NSDate object.
Assuming your two times are instances of NSDate, that might look like this:
NSTimeInterval intA = [timeA timeIntervalSince1970];
NSTimeInterval intB = [timeB timeIntervalSince1970];
NSDate *avgTime = [NSDate dateWithTimeIntervalSince1970:(intA + intB) / 2.0];

Related

How can validate NSDATE

How to find one text field value is within past 60 day excluding current date.
For example if I enter value in text field is 20-July-2012 using Date Picker.Then I click submit,it'll check that specific is date is within 60 days or not. If the values are entered which is before 60 days an alert message is displayed. The values are retrieved from api.
NSDate *today = [NSDate date];
NSTimeInterval dateTime;
if ([pickerDate isEqualToDate:today]) //pickerDate is a NSDate
{
NSLog (#"Dates are equal");
}
dateTime = ([pickerDate timeIntervalSinceDate:today] / 86400);
if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val
{
NSLog (#"Past Date");
}
else
{
NSLog (#"Future Date");
}
Change the value of 86400 to suit your query.In this case, it is the number of seconds we want to compare.
First, convert the text into an NSDate. Then use
timeIntervalSinceDate:[NSDate dateWithTimeIntervalSinceNow:0]
There are a couple of ways to convert text into an NSDate. You can format the text correctly and then use dateWithString or you can convert everything into numbers, multiply them out, and one of the dateWithTimeInterval methods.
If you want the user to be able to enter "July" (plain text month) then you might want to write a method that converts months into their numerical equivalents with string matching.
NSDate *lastDate; //your date I hope you have created it
NSDate *todaysDate = [NSDate date];
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
NSTimeInterval dateDiff = lastDiff - todaysDiff; // number of seconds
int days = dateDiff/(60*60*24); // 5.8 would become 5 as I'm taking int
How do you define 60 days?
You may want to use NSCalendar -dateByAddingComponents:toDate:options: to ensure your 60 days really are 60 days.
NSCalendar also provides -components:fromDate: and -dateFromComponents: which are very nice when dealing with date components.
If 60 days do not need to be true calendar days (daylight saving time switches, astronomical time corrections, stuff like that), you can just have fun with NSDate and the time interval methods alone.

How to see if a date is beween two other dates, and the time used and the time remaining?

If I have set dates like Sunday Jan.29, 2012 2:00:00 PM and Friday Feb.3 2012 5:00:00 PM,
and get the present time, how to I get the spent time from the first date and the present and how do I get the remaining time from the present and the future date?
I have code to show but it is all wrong. There has to be a easy way to do it that I just cant see.
Thank you
Eric
You want to use functions from NSDate
for example:
//get time difference between someDate and now
NSTimeInterval diff = [someDate timeIntervalSinceNow];
//get difference between dates
NSTimeInterval diff2 =[someDate timeIntervalSinceDate: otherDate];
//comparing dates
NSDate * earlierDate = [someDate earlierDate: otherDate];
NSDate * laterDate = [someDate laterDate: otherDate];
If you have your dates available as NSDate objects, you can use timeIntervalSinceDate: to calculate the difference in seconds.
NSTimeInterval sinceThen = [firstDate timeIntervalSinceDate:[NSDate date]];
which will give you the time difference as an NSTimeInterval which is basically a double specifying the time in seconds. If the interval is negative, then firstDate is before now (which is the result of [NSDate date] otherwise it is in the future. If your date is not yet in NSDate form, you might employ an NSDateFormatter to do this (See here, parsing date strings).

NSDate : Add a day based on Time

In my app, I am getting a Date and start time of the same date and end time. The end time could be the time of very next day.
example : Date : 11/01/2011
start time : 5:30 PM
end time : 4:30 AM (of next day morning)
I am not getting the End Date.
How can I add one day into the start date based on the end time. I've referred many answers at SO but could not find the solution.
you should use this function
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];
here 86400 is 60(seconds)*60(minutes)*24(hours).If your startDate is not from current date then you can use this method
- (id)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)anotherDate
now you are getting like this
example : Date : 11/01/2011
start time : 5:30 PM
end time : 4:30 AM (of next day morning)
so you can have two dates, represents start time and end time. Use below method to get time interval
- (NSTimeInterval)timeIntervalSince1970
now you have two time interval for two different dates, right? Now simply subtract it and you will get what you want.
Solved : according to Akkis's answer...
NSDate *startDate = [df dateFromString:startingDateTime];
NSDate *endDate = [df dateFromString:endingDateTime];
NSComparisonResult result;
result = [startDate compare:endDate];
if(result == NSOrderedDescending)
{
NSLog(#"newDate is greater");
endDate = [endDate dateByAddingTimeInterval:60*60*24];
}
Dirty (NO DST check) method: It's easier if you convert everything to common units (i.e. NSTimeInterval / double seconds). Check if end time is less than start time (converted to seconds from midnight). Subtract startSeconds from endSeconds, if result is negative add 24 hours worth of seconds (24*60*60 = 86400). Add result to start date/time.

Best way to store a 'time' value

My class needs two properties: startTime and endTime. What is the best class to use? I know there is NSDate, but I only need to store a specific time (something in between 00:00-23:59), I don't need a date. What is the most elegant solution here?
NSTimeInterval is probably good enough for this.
It stores a time value in seconds as a double.
Eg. 5 mins = 300.0
I believe the most elegant solution, and what you want, is NSTimeInterval, that is the primitive type that NSDate is built on top.
NSTimeInterval is a typedef for double, and is a measurement of time in seconds. This primitive time type do not have any concept of a reference date. What NSDate do is to add this concept of reference date and anchor the 0.0 time at 1 January 2001 GMT. There is nothing that stops you from inventing your own reference date or anchor, like for example "midnight of whatever day there is".
What you can do is to add two properties of the NSTimeInterval either as startTime and endTime and let them both use midnight as the reference. Or you could skip endTime and go for a startTime and duration combo.
There's NSDateComponents, which "can also be used to specify a duration of time, for example, 5 hours and 16 minutes."
The NSDate class is similar to the DateTime class in C#: both hold a date and time, but they can be independent of each other. In Cocoa, you would compare two NSDate classes:
//Create NSDate objects in the time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *startTimeString = #"00:00:00"; //0 seconds
NSString *endTimeString = #"00:00:52"; //52 seconds
NSDate *startTime = [dateFormatter dateFromString:startTimeString];
NSDate *endTime = [dateFormatter dateFromString:endTimeString];
//Compare the time
BOOL date1before2 = [startTime compare:endTime] == NSOrderedAscending;

In iPhone, how to calculate the passed time?

I want know the passed time that also is between two date,
for example , dateTime1 and dateTime2 , I want a function like getPassedTime( time1, time2)
The function result should be like 1 day 3 hours 12 minutes 10 second or just 40 second.
I know that the method timeIntervalSinceDate of the class NSDate can be use, but the problem is convert to details string that could be easy understand.
Anyone can help? share some code will be appreciate or give me some tip if possible.
Use the timeIntervalSinceDate method of the NSDate class:
NSDate *date1 = [NSDate dateWithString:#"2001-03-24 10:45:32 +0600"];
NSDate *date2 = [NSDate dateWithString:#"2001-03-28 12:38:52 +0600"];
NSTimeInterval *timePassed = [date2 timeIntervalSinceDate:date1];
You could use NSCalendar and NSDateComponents.
Code snippet in this thread.