当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Timestamp in Objective-C

问题描述:

I have date, for example: 2012-02-07 14:09:46.109950 and I need timestamp as 1328623786.568740, there is my code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];

[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate *date = [dateFormat dateFromString:@"2012-02-07 14:09:46.109950"];

NSTimeInterval interval = [date timeIntervalSince1970];

[dateFormat release];

NSLog(@"%f", interval);

But now, I have such result 1328623786.110000, .110000 - is rounded. Please help me to get not rounded result.

网友答案:

function CFAbsoluteTimeGetCurrent();

or CFDateGetTimeIntervalSinceDate(CFDateRef theDate, CFDateRef otherDate);

or CFDateGetAbsoluteTime(CFDateRef theDate);

UPDATE: your value automatically rounded here NSDate *date = [dateFormat dateFromString:@"2012-02-07 14:09:46.109950"];

NSLog(@"%@", [dateFormat stringFromDate:date]);

result:

2012-02-07 14:09:46.110000
分享给朋友:
您可能感兴趣的文章:
随机阅读: