Home > Enterprise >  What's Swift current time Date object precision when it is initiated using the default construc
What's Swift current time Date object precision when it is initiated using the default construc

Time:10-10

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.

CodePudding user response:

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

  • Related