views:

616

answers:

1

I want to convert a float to a NSDate

I converted a NSDate into a float using this:

// Turn the date into Integers 
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  
NSCalendarUnit unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;  
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:nsdate_wakeTime];  
NSInteger hour = [dateComponents hour];  
NSInteger min = [dateComponents minute];  

//Convert the time in 24:60 to x.x format.
float myTime = hour + min/60;

after some math stuff I do on the mytime variable i get a bunch of other times in the same float format.

How do I turn a float into a NSDate?

Thanks!

+2  A: 

If you convert the time you've computed to seconds (so, mytime * 60), then you can use dateWithTimeIntervalSinceReferenceDate: to get back to an NSDate. From the math you are doing, it looks like the referenced date here would be 00:00 for the day in question. As Jason mentioned though, there's probably a better way to do what you are trying to accomplish.

Also, you need to change your "myTime" computation to dividing by 60.0 if you actually want the minutes; your sample code is dividing an integer value less than 60 by the integer value 60, which will always be 0.

smorgan
Ok, so I have the user pick two times from datepickers. From that point I want to do a bit of math on the times. So what steps should I take to make my life easy (can I see what the hour value is straight from the NSDate?). I have never used the NS libraries and am new to objective-c, so the answer may be staring me in the face but...
michael
Without any idea what "a bit of math" is, there's no way to suggest an approach. If you are just trying to get the difference between the dates, timeIntervalSinceDate: will give you the difference in seconds.If you explain specifically what you want to accomplish, someone can probably tell you a good way to do it.
smorgan