converting an float to NSDate
-
20-08-2019 - |
Question
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!
Solution
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.