Get day,Month and year from date by function iOS swift
Problem:
I want the functions that will return the day,month or year.How to get that?
Solution:
extension Date {
func getDay() ->Int {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: self)
let day = components.day
return day!
}
func getMonth() -> Int {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: self)
let month = components.month
return month!
}
func getYear() -> Int {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: self)
let month = components.month
return month!
}
}
Call the above functions as like below.
let day = yourdate.getDay()
let month = yourdate.getMonth()
let year = yourdate.getYear()
Comments
Post a Comment