I want to get an array of months from Calendar component in swift. Like:
var monthComponents = ["Jan", "Feb", "Mar" ..]
But I'm only able to get whether the current date or convert a certain date format to string. So, how could I get all month components (month names). Please suggest me with a simple way.
The DateFormatter
class has what you need:
let formatter = DateFormatter()
let monthComponents = formatter.shortMonthSymbols
See the docs for DateFormatter
to see all of the other related methods for getting other various symbols.
Or as of iOS 10/macOS 10.12 you can use Calendar
:
let monthComponents = Calendar.current.shortMonthSymbols
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