I am writing an app which need to show dates with corresponding time symbols like "hours", "minutes" etc. There are cool localized constants for months and weekdays names in NSDateFormatter like monthSymbols (January, February etc). However, I can't find anything like this for such symbols as "hour", "minute" itself. Does such constants exists or I should create and localize these symbols by myself?
UPD:
My goal is a text for label under "30" - only localized "MIN" string without any numbers I should get rid of before placing text from date formatter in the label at the bottom.
Depending on the details of what you need, NSDateComponentsFormatter
might be the solution. It includes NSDateComponentsFormatterUnitsStyleSpellOut
, which renders strings in formats like “One hour, ten minutes”.
Update: After reading comments I'm still not sure what kind of formatting you really need. But hopefully this will be a useful example:
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyle.Full
formatter.allowedUnits = [ NSCalendarUnit.Minute, NSCalendarUnit.Hour ]
let date = NSDate()
let dateComponents = NSCalendar.currentCalendar().components([.Minute, .Hour], fromDate: date)
let dateString = formatter.stringFromDateComponents(dateComponents)
At this point, dateString
will be something like "15 hours, 11 minutes".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With