简体   繁体   English

从特定年份的周数获取数据

[英]Get dats from week number of particular year

I need one function where i will pass any week number and year, that function should return dates for that week.我需要一个函数,我将在其中传递任何周数和年份,该函数应返回该周的日期。 Like if I pass week 2 and year 2016 then function should return me 3 January to 9 January.就像我通过第 2 周和 2016 年一样,函数应该在 1 月 3 日至 1 月 9 日返回我。

Just a very simple solution using NSCalendar functionality:只是一个使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM