-1

What's the current time Date object time precision when it is initiated using Date()? Does it capture the time to milliseconds?

let currentTime = Date()
print(currentTime) // 2022-10-09 09:13:39 +0000

When I print the date it only shows 2022-10-09 09:13:39 +0000 so I wonder if its precision is only to the second.

XY L
  • 25,431
  • 14
  • 84
  • 143

1 Answers1

3

Does it capture the time to milliseconds?

Yes, it does. printing a date shows a fixed string description omitting the fractional seconds. A hint is that TimeInterval is defined as a floating point type (Double).

You can prove it

let interval = Date().timeIntervalSince1970
print(interval)

which shows a real fractional part rather than a Is-floating-point-math-broken value like .00000003

vadian
  • 274,689
  • 30
  • 353
  • 361
  • This answer is only accurate when close enough to 2000. Millisecond precision will be lost at astronomic scale. –  Oct 09 '22 at 11:11