Pregunta

I have a float value, what i need is convert my float to date. For example i have float 50,55, i want to put on a label value like this: 50 years 6 month 6 hours 6 mins 30,04 sec.

And i wonder how to make this label change every 0.01 second for change value. Please provide me solution to convert my float value to date format, any help would be appreciated.

This is code i use for create my float:

-(float)calculationResult{

    lifeTime = (kCountry +kEdu - kBirth - kSmo -kAlco+kDis+kHap+kDri+kMar)*kWei*kEnv*kSle;

    return lifeTime;
}
¿Fue útil?

Solución

The following code splits the given number of "fractional years" into years, month, days and hours. It makes the simplifying assumptions that every year has 365 days, and that one month is exactly 1/12 of a year.

float tmp = lifeTime;
int years = tmp;
tmp =  (tmp - years) * 12.;
int months = tmp;
tmp = (tmp - months) * 365./12.;
int days = tmp;
tmp = (tmp - days) * 24.;
int hours = tmp;

NSString *str = [NSString stringWithFormat:@"%d years %d month %d days %d hours ",
                 years, months, days, hours];

To update a label regularly, you can use the NSTimer class (but updating every 1/100 second is much too often).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top