How to get the month name from a date in Swift

1 Answer

0 votes
import Foundation

extension Date {
    func monthName() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM" // Use "MMM" for abbreviated month name
        return dateFormatter.string(from: self)
    }
}


let date = Calendar.current.date(from: DateComponents(year: 2025, month: 2, day: 8))!
let monthName = date.monthName()

print(monthName) 



/*
run:

February

*/

 



answered Jan 8, 2025 by avibootz
...