Skip to content

Instantly share code, notes, and snippets.

@SketchySwift
Created December 14, 2016 19:27
Show Gist options
  • Save SketchySwift/f9d7273f8bdb916c854ce7110797ce39 to your computer and use it in GitHub Desktop.
Save SketchySwift/f9d7273f8bdb916c854ce7110797ce39 to your computer and use it in GitHub Desktop.

##Getting the day number of the month

var day = ""
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd"
day = "\(formatter.string(from: date))"

#####Output: |14| -> which is the 14th day of December

##Getting month of the year with name

var month = ""
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MM"
let monthInt = Int(formatter.string(from: date))
let monthName = formatter.monthSymbols[monthInt! - 1]
month = "\(monthName)"

#####Output: December

##Getting day of the week name

var dayOfWeekName = ""
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "EEEE"
dayOfWeekName = "\(formatter.string(from: date))"

#####Output: Wednesday

##Getting year

var year = ""
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy"
year = "\(formatter.string(from: date))"

#####Output: 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment