Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a variable with current year in Swift

Tags:

date

swift

How can I set the variable year to the current year in Swift?

let year = ... 
like image 1000
Anton Toni Schffler Avatar asked Feb 07 '18 17:02

Anton Toni Schffler


2 Answers

Use Calendar and Date:

let year = Calendar.current.component(.year, from: Date()) 
like image 58
rmaddy Avatar answered Oct 10 '22 20:10

rmaddy


let date = Date() let calendar = Calendar.current  let year = calendar.component(.year, from: date) let month = calendar.component(.month, from: date) let day = calendar.component(.day, from: date) 
like image 38
Taimoor Suleman Avatar answered Oct 10 '22 21:10

Taimoor Suleman