56

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.

4
  • Do you want the ordinal number as string, the month name (in which language) or something else? And consider to update to Swift 3. That's the future.
    – vadian
    May 6, 2017 at 13:08
  • @vadian 3 is already past. We are at 3.1
    – Leo Dabus
    May 6, 2017 at 16:09
  • @LeoDabus Of course I mean Swift 3 in terms of like a major upgrade.
    – vadian
    May 6, 2017 at 16:15
  • @vadian I am just showing how important is for him to upgrade as soon as possible :)
    – Leo Dabus
    May 6, 2017 at 16:17

3 Answers 3

111
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let nameOfMonth = dateFormatter.string(from: now)
4
  • 7
    The correct format for stand alone month is LLLL userguide.icu-project.org/formatparse/datetime
    – Leo Dabus
    May 6, 2017 at 16:07
  • Is it possible to set the language of the returned month? I tried using locale but it doesn't work. I want to get the current language that is set on iPhone
    – mikro098
    May 10, 2018 at 22:01
  • 5
    @codddeer123 The DateFormatter uses the device's locale by default. You can override it by setting a different locale of course: dateFormatter.locale = Locale(identifier: "es") May 23, 2018 at 5:53
  • 1
    If you are displaying something more than just the month, for example month and day, this might be prone to issues. Better to use .setLocalizedDateFormatFromTemplate to account for possible localization format order differences.
    – craft
    Apr 8, 2022 at 19:33
48

If you are using Swift 3.0 then extensions and Date class are great way to go.

try below code

extension Date {
    var month: String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM"
        return dateFormatter.string(from: self)
    }    
}

Get work with it like below:

 let date = Date()
 let monthString = date.month
4
  • i am using swift 2
    – iron
    May 6, 2017 at 14:32
  • just get on Swift 3.0 if you can bro. bcz its a lot happening on latest swift May 6, 2017 at 18:16
  • 1
    Swift 4.0 Solution: DateFormatter().monthSymbols[currentMonth - 1]
    – cspam
    Apr 12, 2019 at 17:50
  • I would change it to static var month: String { .... } so it can be accessed without having to create an instance. Sep 16, 2022 at 6:21
15

Swift 3.0 and higher

You use DateFormatter() see below for this used in an extension to Date.

Add this anywhere in your project in global scope.

extension Date {
    func monthName() -> String {
            let df = DateFormatter()
            df.setLocalizedDateFormatFromTemplate("MMM")
            return df.string(from: self)
    }
}

Then you can use this anywhere in your code.

let date = Date()
date.monthName() // Returns current month e.g. "May"
1
  • should i use NSdate date is undeclared ....swift 2
    – iron
    May 6, 2017 at 13:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.