Swift 3 获取当前日期是第几周
2017-11-09 本文已影响30人
z小志
func GetWeekByDate(date:Date) ->String{
guard let calendar = NSCalendar(identifier: NSCalendar.Identifier.gregorian) else {
return ""
}
let components = calendar.components([.weekOfYear,.weekOfMonth,.weekday,.weekdayOrdinal], from: date)
//今年的第几周
let weekOfYear = components.weekOfYear!
//这个月第几周
let weekOfMonth = components.weekOfMonth!
//周几
let weekday = components.weekday!
//这个月第几周
let weekdayOrdinal = components.weekdayOrdinal!
print(weekOfYear)
print(weekOfMonth)
print(weekday)
print(weekdayOrdinal)
return "第\(components.weekOfYear!)周";
}