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 strFraction = String(format: "%02d", dateComponents.nanosecond)
timeLabel.text = "\(strFraction)"
How can I get milliseconds from the NSCalendarUnit?
As there is no Calendar.Component.millisecond
, as opposed to Calendar.Component.nanosecond
, we would need to convert from nanoseconds to milliseconds. 1 millisecond is 1000000 nanoseconds, so you would need to divide the number of nanoseconds by 1000000. If you want an integer value, round(nanoseconds/1000000)