Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's Swift current time Date object precision when it is initiated using the default constructor?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading