将浮点数转换为 NSDate [英] converting an float to NSDate
问题描述
我想将浮点数转换为 NSDate
I want to convert a float to a NSDate
我使用以下方法将 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;
在我对 mytime 变量做了一些数学运算之后,我得到了许多相同浮点格式的其他时间.
after some math stuff I do on the mytime variable i get a bunch of other times in the same float format.
如何将浮点数转换为 NSDate?
How do I turn a float into a NSDate?
谢谢!
推荐答案
如果您将计算的时间转换为秒(即 mytime * 60),那么您可以使用 dateWithTimeIntervalSinceReferenceDate:
回到 NSDate
.根据您正在做的数学计算,此处引用的日期似乎是相关日期的 00:00.不过正如杰森所说,可能有更好的方法来完成您想要完成的任务.
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.
此外,如果您确实需要分钟,则需要将myTime"计算更改为除以 60.0;您的示例代码将小于 60 的整数值除以整数值 60,该整数值始终为 0.
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.
这篇关于将浮点数转换为 NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!