Convert time to float - objective-c

What would be the best algorithm / code to convert time to float? I'm doing this in Obj-c but really all I need is an algorithm and I can make the code from there.
EX:
1:30am to 1.50
2:15pm to 14.25
5:45pm to 17.75
EDIT:
My time format is minutes since midnight.

You can parse the string to get two int numbers and an am/pm part (h, m, and a), and then calculate the fraction as follows:
int h, m, a;
// set a to 0 for "am", or 1 for "pm"
float time = 60*(a*12+h)+m;
float res = time / 60.0;

How about NSDate timeIntervalSinceDate? And divide by 3600, of course.
Or, for time within a single day, use NSDateFormatter with an "A" date format to produce milliseconds since midnight, then mash on that.

Related

This code gives you tomorrow after midday

Consider this:
Dim n as Long
n = Now
Why does this give you tomorrow after midday?
It's because Now returns a double to represent the time of day as well as the day itself. The fractional part is used to model the time of day and at noon that value is exactly one half.
Also, the implicit cast to Long rounds the value. This is unlike the casts of c and c++ which truncate the fractional part.
You could use Fix(Now) to truncate.

Convert Float value to Hours and Minutes

I have a float value, for example 0.999888, which I am getting from SQL database.
I have a variable in vbscript assigned to the float value from the SQL.
Lets say that I have this, for example
Dim TimeInFloat
TimeInFloat = 0.999888
I want to convert it to the hours and minutes either in SQL itself or VBscript.
Any suggestions?
You can convert it to hours and minutes in SQL by doing:
select floor(TimeInFloat * 24) as hours,
60 * (TimeInFloat * 24 - floor(TimeInFloat * 24)) as minutes
You can do similar logic in VBA.
In VBScript the CDate function allows you to convert float/double to time:
>>> WScript.Echo CDate(0.999888)
23:59:50

Time in objective c returns as 0

I'm working on a counter, and this is my code to find the time.
NSDate *start = [NSDate date];
NSTimeInterval timeInterval = [start timeIntervalSinceDate:start];
NSLog(#"%f",timeInterval);
This is what it returns over and over again.
2014-03-08 17:59:46.834 Time[67444:303] 0.000000
What is happening with this?
You are using timeIntervalSinceNow on an object that represents a very very close value to "now". Since you print in float, the value is rounded down to lower precision and you get 0.
Are you sure you didn't mean timeIntervalSince1970?
NSTimeInterval is defined as a double. If you need precision above float, try printing as double using %ld. If you need even more precision, look into mach_absolute_time().

NSDateFormatter string format for showing milliseconds

I need to retrieve the current date with this format:
yyyy-MM-dd-hh-mm-ss-XXX
But which are the letters to use for the milliseconds?
As is documented by Unicode, S means fractional second:
Fractional Second - rounds to the count of letters. (example is for 12.34567)
Use as many S as needed for the given precision.
The letters for miliseconds are SS, so you should do:
yyyy-MM-dd-hh-mm-ss-SSS

how to find the difference between two dates in HH:MM:SS format in objective c?

I think the heading itself says what i want to do?
Stil,let me clarify. I am right now working on an application where i have two dated in a format like :
"YYYY:MM:DD HH:MM:SS"
Using which i need to calculate difference between the both dates in the format :
HH:MM:SS
I searched in the wiki and also tried my luck but in vain.Can anybody help me out?
Thanks in advance.
Here's the steps that I think you'll need to perform:
Parse both the dates into NSDate objects using an NSDateFormatter.
Calculate the difference between the two dates using
NSTimeInterval ti = [laterDate timeIntervalSinceDate:earlierDate];
The ti variable above now contains the number of seconds between the two dates. You can reformat that into hours, seconds and minutes with:
NSUInteger h, m, s;
h = (ti / 3600);
m = ((NSUInteger)(ti / 60)) % 60;
s = ((NSUInteger) ti) % 60;
NSString *str = [NSString stringWithFormat:#"%lu:%02lu:%02lu", h, m, s];
Disclaimer
It is late at night for me so the above calculations for hours, minutes and seconds may not be correct.