I'm trying to get milliseconds from NSCalendarUnit. I was able to get nanosecond, but that has way too many numbers. All I want is 2 numbers. Here is my code:
var startDate = NSDate()
let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitNanosecond, fromDate: startDate, toDate: NSDate(), options: nil)
let millisecond = Double(dateComponents.nanosecond)/1000000
let strFraction = String(format: "%02d", round(millisecond)
timeLabel.text = "\(strFraction)"
timeLabel
shows 00, which is not what it's supposed to show. How can I get milliseconds to show from NSCalendar?
The following worked for me (some XCode7 changes...):
var startDate = NSDate()
let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.Nanosecond, fromDate: startDate, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
let millisecond = Int(Double(dateComponents.nanosecond)/1000000 + 0.5)
let strFraction = String(format: "%02d", millisecond)