How do I show microseconds of a date object?

How do I get the microseconds of an object of type Date? I'm open to any suggestions. I'm not able to set the dateFormat property of DateFormatter to be able to show microseconds.

Answered by KMT in 281012022

Maybe some help here - just keep in mind it's from over a year ago, so be ready to update, etc.


https://stackoverflow.com/questions/37126257/swift-full-date-with-milliseconds

Accepted Answer

Maybe some help here - just keep in mind it's from over a year ago, so be ready to update, etc.


https://stackoverflow.com/questions/37126257/swift-full-date-with-milliseconds

I did manage to gets as accurate as nanoseconds using a uint64_t value by getting an NSDate * with timeIntervalSince1970 multiplied by 1000000000. I did testing it by calling [NSDate date] 9 times in a row, confirm that interval between call are around 2-3 microseconds on a device with a old Apple A4 chip.

I did run the same test on a recent device with an Apple A15 chip and calling [NSDate date] 15 times in a row, returns many times similars values. But time precision still arround 1,2 microsecond when change occurs. So, using microseconds, as timestamp is possible with a uint64_t value. But using it to uniquely identify an element on a DBase required a bit more work then relying on just doing another [NSDate date] call.

A uint64_t value can hold up to 0,1 nanosecond value. Not more (multiplying timeIntervalSince1970 by 10,000,000,000 is the maximum). Use @"..NSDate is: %llu" in a NSLog to show uint64_t value.

So using a microsecond values as timestamp seams to be the right one to use.

How do I show microseconds of a date object?
 
 
Q