How to convert float to date format? - iphone

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;
}

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).

Related

Compress date to unique alphanumeric characters

If I have a date YYMMDDHHmmss such as 190525234530 how do I work out the smallest number of characters to represent this using 0-9a-z (36 characters)?
I believe there are 3,153,600,000 combinations (100 years * 365 days * 24 hours * 60 minutes * 60 seconds) which would fit into 32 bits. Does this mean I could represent these dates using 4 characters?
I am a bit lost as to how to do the conversion so if anyone could show me the maths that would be greatly appreciated.
I ended up doing this in JavaScript, I decided I wanted to compress to 6 characters so I created my own time which generates unique ID's for up to 68 years from 01/01/2019 which worked for me.
function getId() {
//var newTime = parseInt(moment().format("X")) - 1546300800;//seconds since 01/01/2019
var newTime = Math.floor((new Date()).getTime() / 1000) - 1546300800;//seconds since 01/01/2019
var char = "0123456789abcdefghijklmnopqrstuvwxyz";//base 36
return char[Math.floor(newTime / 36**5)]+
char[Math.floor(newTime%36**5 / 36**4)]+
char[Math.floor(newTime%36**4 / 36**3)]+
char[Math.floor(newTime%36**3 / 36**2)]+
char[Math.floor(newTime%36**2 / 36)]+
char[Math.floor(newTime%36)];
}
console.log(getId());
Thanks to #user956584 this can be be changed to:
function getId() {
//var newTime = parseInt(moment().format("X")) - 1546300800;//seconds since 01/01/2019
var newTime = Math.floor((new Date()).getTime() / 1000) - 1546300800;//seconds since 01/01/2019
return newTime.toString(36);
}
console.log(getId());

Displaying an integer from a string using the minute , second and isecond time format

Please can anyone help me out here, I wish to display the following:
for (int i = 0; i < [highScores count]; i++)
{
[scoresString appendFormat:#"%i. %i\n", i + 1, [[highScores objectAtIndex:i] intValue]];
}
As a time value in this format 00:00:00 (in minutes seconds and hundredths of a second). At the moment I am getting values such as 7008(note that i have my maximum time in seconds is defined as *# define MAX_TIME 7200*).
Please how do about doing this conversion.
As X Slash states, if your integer is in seconds then you can't get hundredths of a second, but to convert it to minutes and seconds, do this:
int total = [[highScores objectAtIndex:i] intValue];
int minutes = total / 60;
int seconds = total % 60;
[scoresString appendFormat:#"%i. %02i:%02i\n", i + 1, minutes, seconds];
Note the 02 between the % and i - that means space out the number to two characters and insert leading zeros.
Incidentally, if your seconds are coming out as 7008 then with the logic above you'll get a time of 116:48 - i.e. 116 minutes, 48 seconds. Are you sure your time is in seconds? If it is you may wish to add an hours column.

Format a float value to get the digits before a decimal point

In my application I have a music player, which plays music with a length of 0:30 seconds.
However in a UILabel I am currently displaying the progress, and as it is a float, the label is displaying i.e 14.765.
I would appreciate it if you could tell me how I could get the label to display
0:14 rather than 14.765.
Also, I would appreciate it if you could tell me how I could display 0:04 if the progress was 4seconds in.
This works properly:
float time = 14.765;
int mins = time/60;
int secs = time-(mins*60);
NSString * display = [NSString stringWithFormat:#"%d:%02d",mins,secs];
Results:
14.765 => 0:14
30.000 => 0:30
59.765 => 0:59
105.999 => 1:45
EDIT
In addition the 'one liner':
float time = 14.765;
NSString * display = [NSString stringWithFormat:#"%d:%02d",(int)time/60,(int)time%60];
You first need to convert your float to an integer, rounding as you wish. You can then use the integer division, /, and remainder, % operations to extract minutes and seconds and produce a string:
float elapsedTime = 14.765;
int wholeSeconds = round(elapsedTime); // or ceil (round up) or floor (round down/truncate)
NSString *time = [NSString stringWithFormat:#"%02d:%02d", wholeSeconds/60, wholeSeconds%60];
The %02d is the format specification for a 2-digits, zero padded, integer - look up printf in the docs for full details.
//%60 remove the minutes and int removes the floatingpoints
int seconds = (int)(14.765)%60;
// calc minutes
int minutes = (int)(14.765/60);
// validate if seconds have 2 digits
NSString time = [NSString stringWithFormat:#"%i:%02i",minutes,seconds];
that should work. Can't test it i'm on Win currently

UIDatePicker - Difference between 2 dates in hours:minutes

I have this
NSDate *date1Val = date1.date;
NSDate *date2Val = date2.date;
NSTimeInterval interval = [date2Val timeIntervalSinceDate:date1Val];
int hours = (int)interval / 3600; // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
NSString *timeDiff = [NSString stringWithFormat:#"%d:%d", hours, minutes];
but it doesnt show me correct date - for 24 hours it says 23:57 no idea why
When selecting a date using a UIDatePicker in date only mode the time that was set in date + time mode is also present in the supplied date. You can go into IB, switch the UIDatePickers to Date & Time mode and make sure that the hour and minute are the same.

Can this be done using LINQ/Lambda, C#3.0

Objective: Generate dates based on Week Numbers
Input: StartDate, WeekNumber
Output: List of dates from the Week number specified till the StartDate
i.e. If startdate is 23rd April, 2010 and the week number is 1, then the program should return the dates from 16th April, 2010 till the startddate.
The function
public List<DateTime> GetDates(DateTime startDate,int weeks)
{
List<DateTime> dt = new List<DateTime>();
int days = weeks * 7;
DateTime endDate = startDate.AddDays(-days);
TimeSpan ts = startDate.Subtract(endDate);
for (int i = 0; i <= ts.Days; i++)
{
DateTime dt1 = endDate.AddDays(i);
dt.Add(dt1);
}
return dt;
}
I am calling this function as
DateTime StartDate = DateTime.ParseExact("20100423", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
List<DateTime> dtList = GetDates(StartDate, 1);
The program is working fine.
Question is using C# 3.0 feature like Linq, Lambda etc. can I rewrite the program.
Why? Because I am learning linq and lambda and want to implement the same. But as of now the knowledge is not sufficient to do the same by myself.
Thanks.
Something like this:
public IEnumerable<DateTime> GetDates2(DateTime startDate, int weeks)
{
var days = weeks * 7;
return Enumerable.Range(-days, days + 1).Select(i => startDate.AddDays(i));
}
The Enumerable.Range method will return a sequence of integers within a specified range, in your example from -7 to 0.
After that I simply use each integer to substract that number of days from your initial startDate, building an IEnumerable<DateTime>.
You can try something like
int weeks = 1;
var days = from d in Enumerable.Range(-weeks * 7, weeks * 7 + 1)
select StartDate.AddDays(d);