user2105008 user2105008 - 7 months ago 24
Swift Question

iOS : How to calculate total number of week in selected year

How can we calculate total number fo week in selected year?

suppose i select year 2000 then it should be calculate total number of year in 2000 year.

thanks in advance.


try this

    let weekRange = NSCalendar.currentCalendar().rangeOfUnit(.WeekOfYear, inUnit: .Year, forDate: Date.parse("1997"))
    print("\( Int (weekRange.length))")

class Date {
class func parse(dateStr:String, format:String="yyyy") -> NSDate {
    let dateFmt = NSDateFormatter()
    dateFmt.timeZone = NSTimeZone.defaultTimeZone()
    dateFmt.dateFormat = format
    return dateFmt.dateFromString(dateStr)!