How can I get the current month as String?

iron picture iron · May 6, 2017 · Viewed 55k times · Source

I need to get may as a current month, but I could not do. How can I achieve this?

   let date = NSDate()
   let calendar = NSCalendar.currentCalendar()
   let components = calendar.components([.Day , .Month , .Year], fromDate: date)
   
   let year =  components.year
   let month = components.month
   let day = components.day

I have done this but does not worked.

Answer

André Slotta picture André Slotta · May 6, 2017
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let nameOfMonth = dateFormatter.string(from: now)