-3

I have 2 DATES.

  1. End Date
  2. 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;
Monika Patel
  • 2,287
  • 3
  • 20
  • 45

1 Answers1

4

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579