On date formatting

My app shows due dates, of course I need proper date formatting. Unfortunately NSDateFormatterStyle is not enough: it won’t let you specify anything about how you want your date to be displayed.

Yet there’s a class method on NSDateFormatter called dateFormatFromTemplate:options:locale: which gives you total control. The fun (and the most useful) thing about it is that you just need to give it components of a date, and it will arrange the format string automatically based on locale. Neat.

Don’t forget to do setDoesRelativeDateFormatting:NO, otherwise you’ll just get a blank string on the output. If you do need relative formatting at the same time with custom date format strings, here you go: NSDateFormatter+RelativeDateFormat.[hm]. I used this as a base for my own customization.

2014   Coursera Due   iOS
Popular