今天是3月6日星期五。如何找到2月16日是开始日,2月22日是前一周前一周的结束日。 16是我的国家保加利亚在美国将是15和21我使用.currentCalendar()
答案 0 :(得分:10)
这样的事情应该有效:
let cal = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.weekOfYear -= 1
if let date = cal.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(0)) {
var beginningOfWeek: NSDate?
var weekDuration = NSTimeInterval()
if cal.rangeOfUnit(.CalendarUnitWeekOfYear, startDate: &beginningOfWeek, interval: &weekDuration, forDate: date) {
let endOfWeek = beginningOfWeek?.dateByAddingTimeInterval(weekDuration)
print(beginningOfWeek) // Optional(2015-02-15 05:00:00 +0000)
print(endOfWeek) // Optional(2015-02-22 05:00:00 +0000)
}
}
答案 1 :(得分:5)
一个更简单的Swift 2替代作为扩展,派生自Martin R's answer for getting the end of the month:
extension: NSDate {
func startOfWeek(weekday: Int?) -> NSDate? {
guard
let cal: NSCalendar = NSCalendar.currentCalendar(),
let comp: NSDateComponents = cal.components([.YearForWeekOfYear, .WeekOfYear], fromDate: self) else { return nil }
comp.to12pm()
cal.firstWeekday = weekday ?? 1
return cal.dateFromComponents(comp)!
}
func endOfWeek(weekday: Int) -> NSDate? {
guard
let cal: NSCalendar = NSCalendar.currentCalendar(),
let comp: NSDateComponents = cal.components([.WeekOfYear], fromDate: self) else { return nil }
comp.weekOfYear = 1
comp.day -= 1
comp.to12pm()
return cal.dateByAddingComponents(comp, toDate: self.startOfWeek(weekday)!, options: [])!
}
}
用法:
// Use 2 for Monday, 1 for Sunday:
print(NSDate().startOfWeek(1)!) // "2016-01-24 08:00:00 +0000\n"
print(NSDate().endOfWeek(1)!) // "2016-01-30 08:00:00 +0000\n"
为了防范DST等,您还应该实施扩展以强制时间到下午12点:
internal extension NSDateComponents {
func to12pm() {
self.hour = 12
self.minute = 0
self.second = 0
}
}
答案 2 :(得分:4)
如果有人在寻找swift 3的答案:
func startOfWeek(weekday: Int?) -> Date {
var cal = Calendar.current
var component = cal.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
component.to12am()
cal.firstWeekday = weekday ?? 1
return cal.date(from: component)!
}
func endOfWeek(weekday: Int) -> Date {
let cal = Calendar.current
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
component.to12pm()
return cal.date(byAdding: component, to: startOfWeek(weekday: weekday))!
}
我设定为当天的00:00:00
internal extension DateComponents {
mutating func to12am() {
self.hour = 0
self.minute = 0
self.second = 0
}
mutating func to12pm(){
self.hour = 23
self.minute = 59
self.second = 59
}
}
答案 3 :(得分:0)
快速4 +:
在Calendar
的帮助下主要使用DateComponents
的非常简单的方法:
Date
Calendar.current.date(byAdding: .weekdayOrdinal, value: -1, to: aDate)
Calendar.current.component(.weekday, from: bDate)
Calendar.current.date(byAdding: .day, value: offset, to: bDate)
extension Date {
var firstWeekdayOfLastWeek: Date? {
guard let previousWeek = Calendar.current.date(byAdding: .weekdayOrdinal,
value: -1, to: self)
else { return nil }
let offsetToFirstWeekday: Int = {
let currentWeekday = Calendar.current.component(.weekday, from: previousWeek)
guard currentWeekday > 0 else { return 0 }
return 1 - currentWeekday
}()
let result = Calendar.current.date(byAdding: .day,
value: offsetToFirstWeekday,
to: previousWeek)
return result
}
var lastWeekdayOfLastWeek: Date? {
guard let previousWeek = Calendar.current.date(byAdding: .weekdayOrdinal,
value: -1, to: self)
else { return nil }
let offsetToLastWeekday: Int = {
let currentWeekday = Calendar.current.component(.weekday, from: previousWeek)
return 7 - currentWeekday
}()
let result = Calendar.current.date(byAdding: .day,
value: offsetToLastWeekday,
to: previousWeek)
return result
}
}
let formatter = DateFormatter()
formatter.dateStyle = .long
let date = Date()
//My current system date (as of time of writing)
print(formatter.string(from: currentDate)) //December 11, 2020
//Results (my system has Sunday as the start of the week)
print(formatter.string(from: date.firstWeekdayOfLastWeek!)) //November 29, 2020
print(formatter.string(from: date.lastWeekdayOfLastWeek!)) //December 5, 2020
强制展开仅用于示例目的