我需要在毫秒级精确时间。我怎么能从NSDate
得到它。目前,当我NSLog
时,它仅显示高达秒。如何以毫秒的精度获得nsdate?
19
A
回答
29
你需要使用下面的方法来转换秒。到毫秒:
([NSDate timeIntervalSinceReferenceDate] * 1000)
4
基斯是对有关毫秒计算,但你可能要考虑仅处理的时间间隔的小数部分,你可以使用NSDateComponents把所有的时间成分精确到秒。如果你使用类似下面的东西,你可以添加一个毫秒组件:
/*This will get the time interval between the 2
dates in seconds as a double/NSTimeInterval.*/
double seconds = [date1 timeIntervalSinceDate:date2];
/*This will drop the whole part and give you the
fractional part which you can multiply by 1000 and
cast to an integer to get a whole milliseconds
representation.*/
double milliSecondsPartOfCurrentSecond = seconds - (int)seconds;
/*This will give you the number of milliseconds accumulated
so far before the next elapsed second.*/
int wholeMilliSeconds = (int)(milliSecondsPartOfCurrentSecond * 1000.0);
希望是有帮助的。
6
对于那些谁想要得到毫秒的时间(如Java中)
double timestamp = [[NSDate date] timeIntervalSince1970];
int64_t timeInMilisInt64 = (int64_t)(timestamp*1000);
相关问题
- 1. 如何清除NSDate毫秒
- 2. 的Java毫秒精度
- 3. 如何设置精度毫秒在C#
- 4. 以毫秒级精度返回time.now
- 5. 如何获得毫秒?
- 6. 如何比较两次以毫秒为单位的精度?
- 7. 转换毫秒的NSDate
- 8. 如何以纳秒精度获得PHP的时间?
- 9. 如何以秒精度获得GPS位置?
- 10. 如何在mysql中获得毫秒
- 11. 如何在C++中获得毫秒?
- 12. Java 8 LocalDateTime.now()仅提供毫秒精度
- 13. 经过时间20毫秒精度
- 14. Java DateFormat为2毫秒精度
- 15. SDL_GetTicks()精度低于毫秒级
- 16. 如何以毫秒级精度获得API调用之间绝对的时间差异?
- 17. ORMLite在MySQL/MariaDB中以毫秒级精度的日期
- 18. java中的定时器,精度为0.1毫秒,间隔为23毫秒
- 19. 如何以2毫秒的精度同步2个系统之间的时间?
- 20. 如何获得两个QDateTimes之差(以毫秒为单位)?
- 21. 如何在100毫秒内获得自动完成响应(毫秒)
- 22. 如何获得毫秒或微秒加载时间在MySQL
- 23. 为什么MySQL不支持毫秒/微秒精度?
- 24. 以毫秒为单位的mp3长度
- 25. 如何将NSDate转换为Objective C中的毫秒数?
- 26. 如何将秒表刻度转换为纳秒,毫秒和秒?
- 27. 在ObjC中获得NSDate秒数
- 28. 打字稿时间戳获得毫秒
- 29. Java Swing.Timer获得实时毫秒
- 30. 如何获得类型的精度?
欲了解更多你可以通过已经(与iOS7和使用的Xcode 5的iPhone模拟器上测试)讨论后http://stackoverflow.com/questions/889380/how-can-i-get-a-precise-time-for-example-in-milliseconds-in-objective-c – Jhaliya 2011-03-19 04:24:11
这无助于提高精度这个日期。你所做的就是把这个值转换成以毫秒为单位。如果精度级别在100 ms之前,则在100 ms之后。所以要小心 - 这个答案具有欺骗性。 – bkbeachlabs 2018-02-20 20:04:17
请阅读问题描述评论前仔细,用户想要转换到毫秒,并且接受了答案。 – Jhaliya 2018-02-22 05:56:13