Skip to content

Instantly share code, notes, and snippets.

@MLKrisJohnson
Last active November 11, 2016 13:50
Show Gist options
  • Save MLKrisJohnson/dca6b8d974ecb92e36c946af96a88de5 to your computer and use it in GitHub Desktop.
Save MLKrisJohnson/dca6b8d974ecb92e36c946af96a88de5 to your computer and use it in GitHub Desktop.
Example of using NSDateFormatter to get/parse month names for user's locale/calendar.
import Foundation
let dateFormatter = DateFormatter()
let calendar = dateFormatter.calendar!
dateFormatter.dateFormat = "MMMM"
let now = Date()
let nowAsString = dateFormatter.string(from: now) // "November"
let september = dateFormatter.date(from: "September")!
let monthNumber = calendar.component(Calendar.Component.month, from: september) // 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment