将NSDate转换为毫秒纪元时间

我需要能够将date转换为时间戳,以毫秒为单位的时间。 所有我在网上看到的转换毫秒NSDate而不是相反。 有什么帮助吗?

NSTimeInterval是一个已经包含小数点后的秒数据的double。 根据你的需要,你的转换可以是一个简单的乘以1000。

- (void)testDateFormat { NSDate *date = [NSDate date]; NSLog(@"Time: %f", floor([date timeIntervalSince1970] * 1000)); NSLog(@"Time: %f", floor([date timeIntervalSince1970])); NSLog(@"Time: %lli", [@(floor([date timeIntervalSince1970] * 1000)) longLongValue]); NSLog(@"Time: %lli", [@(floor([date timeIntervalSince1970])) longLongValue]); } // Result // 2013-04-15 13:28:11.284 TestApp[10469:907] Time: 1366057691284.000000 // 2013-04-15 13:28:11.286 TestApp[10469:907] Time: 1366057691.000000 // 2013-04-15 13:28:11.287 TestApp[10469:907] Time: 1366057691284 // 2013-04-15 13:28:11.288 TestApp[10469:907] Time: 1366057691 

timeIntervalSince1970将返回值乘以1000,因为返回的值不是以毫秒为单位,请检查该网站以进行更多testinghttp://currentmillis.com/

    Interesting Posts