Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS localized string constants for time symbols ("MINUTE(S)", "HOUR(S)" etc.)

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. enter image description here

like image 620
Fyodor Volchyok Avatar asked Dec 03 '22 16:12

Fyodor Volchyok


1 Answers

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".

like image 54
Tom Harrington Avatar answered Jan 07 '23 14:01

Tom Harrington