How to get the current date and time with milliseconds accuracy in Swift

1 Answer

0 votes
import Foundation

func getCurrentDateTime() -> String {
    let now = Date()
    
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
    let formattedDate = formatter.string(from: now)
    
    return formattedDate
}

print(getCurrentDateTime())



 
/*
run:
 
2024-12-07 07:54:15.508
 
*/

 



answered Dec 7, 2024 by avibootz
...