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.
Related
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.
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).
I have an array which contains time in HH:mm:ss format.
I want to convert this into epoch time. To convert a particular time into epoch, date along with time should be passed.
I want to pass today's date along with the time from array.
For example 09:15:30 (date 22/12/2011) which is a string, should be converted into 1324525530000 (corresponding epoch value)
How should I convert this ??
Any help is appreciated.
Maybe you can get an idea looking at this snippet:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] initWithSafeDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSTimeInterval epoch = [date timeIntervalSince1970];
Anyway you can have a look to NSDateFormatter and NSDate in the reference docs.
That looks like an epoch time in milliseconds - as far as I know it's supposed to be in seconds.
You can use NSDateFormatter to parse a string and convert it to a NSDate. This date can then return the seconds since 1970. If you really need it, simply multiply this by 1000.
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];
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;