iosswiftdatensdatenscalendar

How can I get the current month as String?


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.


Solution

  • let now = Date()
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "LLLL"
    let nameOfMonth = dateFormatter.string(from: now)