2016-03-25 82 views
-4

我需要一个函数,我将通过任何一周的数字和年份,该函数应返回该周的日期。就像如果我通过第2周和2016年,那么函数应该会在1月3日到1月9日之间返回。从特定年份的周数中获取数据

+0

见'NSCalendar',它具有完全的功能做到这一点。 – Sulthan

+0

@Sulthan你可以请邮政编码,这将真的有帮助。 –

+0

顺便提一句,2016年1月3日是2016年第一周*的开始。本年的第一周(WN 1)是包含1月4日或本年度的第一个周二的一周。 – Sulthan

回答

1

使用NSCalendar功能只是一个非常简单的解决方案:

let week = NSDateComponents() 
week.yearForWeekOfYear = 2016 // the year 
week.weekOfYear = 10 // week index 

let calendar = NSCalendar.currentCalendar() 

// start of week or nil if the week does not exist 
let weekStart = calendar.dateFromComponents(week) 

// add 1 week to start (this is essentially the start of the next week) 
let weekEnd = calendar.dateByAddingUnit(.WeekOfYear, value: 1, toDate: weekStart!, options: []) 

print(weekStart) 
print(weekEnd) 
+0

tnx男人你真棒。 –

相关问题