Convert seconds to days, minutes, and hours in Obj-c

后端 未结 11 2375
[愿得一人]
[愿得一人] 2020-12-13 01:16

In objective-c, how can I convert an integer (representing seconds) to days, minutes, an hours?

Thanks!

相关标签:
11条回答
  • 2020-12-13 01:59

    I know this is an old post but I wanted to share anyway. The following code is what I use.

    int seconds = (totalSeconds % 60);
    int minutes = (totalSeconds % 3600) / 60;
    int hours = (totalSeconds % 86400) / 3600;
    int days = (totalSeconds % (86400 * 30)) / 86400;
    

    First line - We get the remainder of seconds when dividing by number of seconds in a minutes.

    Second line - We get the remainder of seconds after dividing by the number of seconds in an hour. Then we divide that by seconds in a minute.

    Third line - We get the remainder of seconds after dividing by the number of seconds in a day. Then we divide that by the number of seconds in a hour.

    Fourth line - We get the remainder of second after dividing by the number of seconds in a month. Then we divide that by the number of seconds in a day. We could just use the following for Days...

    int days = totalSeconds / 86400;
    

    But if we used the above line and wanted to continue on and get months we would end up with 1 month and 30 days when we wanted to get just 1 month.

    Open up a Playground in Xcode and try it out.

    0 讨论(0)
  • 2020-12-13 01:59

    You will get days,hours,minutes and seconds from total seconds(self.secondsLeft)

    days = self.secondsLeft/86400;
    hours = (self.secondsLeft %86400)/3600;
    minutes = (self.secondsLeft % 3600) / 60;
    seconds = (self.secondsLeft %3600) % 60;
    

    Code ......

      [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
    
    -(void) updateCountdown {
    
        int days,hours,minutes,seconds;
        self.secondsLeft--;
    
        days = self.secondsLeft/86400;
        hours = (self.secondsLeft %86400)/3600;
        minutes = (self.secondsLeft % 3600) / 60;
        seconds = (self.secondsLeft %3600) % 60;
    
        NSLog(@"Time Remaining =%@",[NSString stringWithFormat:@"%02d:%02d:%02d:%02d",days, hours, minutes,seconds]);
    }
    
    0 讨论(0)
  • 2020-12-13 02:00

    The most preferred way in objective-c might be this one, as recommend by Apple in their doc.

      NSDate *startDate = [NSDate date];
        NSDate *endDate = [NSDate dateWithTimeInterval:(365*24*60*60*3+24*60*60*129) sinceDate:startDate];
    
        NSCalendar *gregorian = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
        NSDateComponents *components = [gregorian components:unitFlags
                                                    fromDate:startDate
                                                      toDate:endDate options:0];
        NSInteger years = [components year];
        NSInteger months = [components month];
        NSInteger days = [components day]; 
        NSLog(@"years = %ld months = %ld days = %ld",years,months,days);
    

    Make sure not to retrieve components not defined with the unitFlags, integer will be "undefined".

    0 讨论(0)
  • 2020-12-13 02:01

    Use the built-in function strftime():

    static char timestr[80];
    char * HHMMSS ( time_t secs )
    {
        struct tm ts;
        ts = *localtime(&secs);
        strftime(timestr, sizeof(timestr), "%a %b %d %H:%M:%S %Y %Z", &ts); // "Mon May 1, 2012 hh:mm:ss zzz"
        return timestr;
    }
    
    0 讨论(0)
  • 2020-12-13 02:02

    Just a bit modification for both compatible with 32 bit & 64 bit. Using NSIntegerthe system will automatically convert to int or long basing on 32/64 bit.

    NSInteger seconds = 10000;
    NSInteger remainder = seconds % 3600;
    NSInteger hours = seconds / 3600;
    NSInteger minutes = remainder / 60;
    NSInteger forSeconds = remainder % 60;
    

    By the same way you can convert to days, weeks, months, years

    0 讨论(0)
提交回复
热议问题