user2105008 user2105008 - 1 year ago 67
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.

Answer Source

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)!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download