converting an float to NSDate

北城余情 提交于 2019-12-07 15:22:12

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/844367/converting-an-float-to-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!