Created
          October 13, 2017 01:03 
        
      - 
      
- 
        Save anvarazizov/2fab5aa77e687f2c35a27ce09d430d8e to your computer and use it in GitHub Desktop. 
    print time in swift with milliseconds
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func printDate(string: String) { | |
| let date = Date() | |
| let formatter = DateFormatter() | |
| formatter.dateFormat = "HH:mm:ss.SSSS" | |
| print(string + formatter.string(from: date)) | |
| } | 
There are many convenient options now:
    let date = Date.now
    let formattedFractional = date.formatted(.dateTime.hour().minute().second().secondFraction(.fractional(3)))
    // example: "8:07:48.145 AM"
    print(formattedFractional)
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This is super useful, but the format should be "HH:mm:ss.SSS" (three big S's). The fourth S puts a 0 at the end of all values.