2011-03-19 75 views

回答

29

你需要使用下面的方法来转换秒。到毫秒:

([NSDate timeIntervalSinceReferenceDate] * 1000) 
+1

欲了解更多你可以通过已经(与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

+0

这无助于提高精度这个日期。你所做的就是把这个值转换成以毫秒为单位。如果精度级别在100 ms之前,则在100 ms之后。所以要小心 - 这个答案具有欺骗性。 – bkbeachlabs 2018-02-20 20:04:17

+0

请阅读问题描述评论前仔细,用户想要转换到毫秒,并且接受了答案。 – Jhaliya 2018-02-22 05:56:13

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);