Convert NSTimeInterval to Days, Hours, Minutes, Seconds [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have 2 DATES.
End Date
Current Date
Now, I want to find NSTimeInterval and Calculate remaining time in Days, Hours, Minutes, Seconds.
I do not want to use NSDateComponents.
I want some formula that calculate that gives remaining time in Days, Hours, Minutes, Seconds.
I tried this below formula but that formula gives remaining Hours, Minutes, Seconds.
But how do I calculate this Days, Hours, Minutes, Seconds ??
I'm using this below code
#property (nonatomic, assign) NSTimeInterval secondsLeft;
_hours = (int)self.secondsLeft / 3600;
_minutes = ((int)self.secondsLeft % 3600) / 60;
_seconds = ((int)self.secondsLeft %3600) % 60;

Some of your calculations are incorrect. You want:
_hours = (int)self.secondsLeft / 3600;
_minutes = (int)self.secondsLeft / 60 % 60;
_seconds = (int)self.secondsLeft % 60;
This assumes _hours, _minutes, and _seconds are of type int (or some other appropriate integer type).
If you want to format the NSTimeInterval into a useful string formatted properly for the user's locale, use NSDateComponentsFormatter:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
NSString *result = [formatter stringFromTimeInterval:self.secondsLeft];

Related

How to get int value of time stamp which includes milliseconds in objective c

NSNumber * uniqueId = [NSNumber numberWithInt:([[NSDate date] timeIntervalSince1970])];
Milliseconds is not included in the above code.If i used like below code ,it is printing negative values.
NSNumber * uniqueId1 = [NSNumber numberWithInt:([[NSDate date] timeIntervalSince1970] * 1000)];
Can we get time stamp with milliseconds as int????
#AnjaniG you can use one of them..
NSString *strTimeStamp = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970] * 1000];
int timestamps = [strTimeStamp intValue];
NSLog(#"number int = %d",timestamps);
You should not use an Int, [[NSDate date] timeIntervalSince1970] * 1000 is bigger than INT_MAX. You should use long long to store the value.
NSNumber * uniqueId1 = [NSNumber numberWithLongLong:([[NSDate date] timeIntervalSince1970] * 1000)];
The problem is that you are casting a floating point to an integer.
As per the documentation, timeIntervalSince1970 returns an NSTimeInterval, which is a double, not an integer.
In your first code example, you're actually discarding the milliseconds by casting.
In your second code example, you are overflowing the integer. After multiplying the value by 1000, it is too large to fit in an integer.
In the end, you're just doing too much code and you shouldn't need to really worry about this.
NSTimeInterval interval = [NSDate date].timeIntervalSince1970;
NSNumber *timestamp = #(interval * 1000.);
Here, I use the proper documented type of NSTimeInterval. Then I multiply that by 1,000 to change seconds to milliseconds. Finally, I use Clang literal syntax to instruct the compiler to create the appropriate NSNumber.

How to compare time greater than 3 sec in ios

How to compare time is greater than 3 sec in ios.
Time is like "2016-05-10 05:31:14". I got code for comparing like greater or less with specific time but how i will achieve it is greater than 3 sec etc.
You can get seconds difference between two dates by
NSDate *someDate;//Some Date
NSLog(#"Seconds --> %f",[[NSDate date] timeIntervalSinceDate: someDate]);
Please try this one. May be Its possible for you:
NSDate* timeNow = [NSDate date];
// If less than 30 seconds, do something
if ([timeNow timeIntervalSinceDate:anEarlierTime] < 30.0f)
{
// Do something
}
- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate
{
NSDate* enddate = checkEndDate;
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
double secondsInMinute = 60;
// secondsBetweenDates is your seconds difference
NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
if (secondsBetweenDates == 0)
return YES;
else if (secondsBetweenDates < 0)
return YES;
else
return NO;
}
Here, secondsBetweenDates is your difference in seconds. You can check that it is smaller or greater or equal than 3.
You can get difference here in hours also!
Hope this will help :)

iOS - How do I convert float time to real time (hh:mm)

I'm calculating the average travel time from one point to another by using the following calculation:
float tSpeed = [[NSString stringWithFormat:#"%f", speed] floatValue];
float duration = totDistance/tSpeed;
tripDuration_Label.text = [NSString stringWithFormat:#"%f", duration];
speed is in miles per hour so the output is in hours.
This gives me a float value for the time, I need to convert it to time (hh:mm).
Thanks
1 hour is 60 minutes so:
NSInteger hours = duration; // 2.5 -> 2
NSInteger minutes = ( duration - hours ) * 60; // ( 2.5 - 2 ) * 60
tripDuration_Label.text = [NSString stringWithFormat:#"%d:%02d", hours, minutes];
Probably you would like to do the conversion to time like in this example but before you need to get the integer values from float.
Fot this you can use modulo to sparate things before and after comma.

How can I get the fileCreationDate to an accuracy of 6 decimal places when converted using timeIntervalSince1970?

How can I get the fileCreationDate for a file stored in the NSDocumentsDirectory to an accuracy of 6 decimal places when converted to an NSTimeInverval?
Some code:
//Not accurate enough:
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:myPhotoPath error:nil];
NSDate *creationDateOfPhoto = [fileAttribs fileCreationDate];
NSTimeInterval creationDateAsTimeInterval = [creationDateOfPhoto timeIntervalSince1970];
NSLog(#"Creation Date of Photo As Time Interval Since 1970: %f", creationDateAsTimeInterval);
//With the desired accuracy:
NSDate* now = [NSDate date];
NSTimeInterval nowStampAsTimeInterval = [now timeIntervalSince1970];
NSLog(#"Now As Time Interval Since 1970: %f", nowStampAsTimeInterval);
A sample output from this code is:
Creation Date of Photo As Time Interval Since 1970: 1373022866.000000
Now As Time Interval Since 1970: 1373022884.294028
Is it possible or is it a limitation of storage in NSDocuments?
It's a OS level limitation. HFS+, the file system which is used by iOS, has a date resolution of one second - this is why all your file timestamps are whole seconds. Sub second precision isn't supported. Sorry!

Trying to do some simple calculations in iOS project?

I want to know how to do some simple equations in my iOS app, if anyone could point me in the right direction that would be wonderful!
I need to know how to convert an NSNumber that represents minutes to an NSString which represents hours and minutes (example: 100 = 1 hour and 40 minutes)
I also want to know if its possible to convert something like 2013-02-08T10:50:00.000 to 10:50AM
Thanks for any tips you guys might have.
For the conversion :
NSNumber *yourNumber = [NSNumber numberWithInt100];
NSInteger hour = [yourNumber intValue] / 60;
NSInteger minutes = [yourNumber intValue] % 60;
NSString *time_stamp = [NSString stringWithFormat:#"%d hour and %d minutes",hour,minutes];
As the comments suggested you can use NSDateFormatter to format your NSDate.

Resources