Swift 3 獲取當前日期是第幾周

z小志發表於2018-01-23
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!)周";
    }
複製程式碼

相關文章