LightNight
LightNight

Reputation: 1625

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.

Upvotes: 3

Views: 4059

Answers (1)

Volodymyr B.
Volodymyr B.

Reputation: 3441

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

Upvotes: 6

Related Questions