如果日期是月初,则获取当前周的所有日期返回不正确
Posted
技术标签:
【中文标题】如果日期是月初,则获取当前周的所有日期返回不正确【英文标题】:Get all dates for current week returns incorrect if the date is beginning of a month 【发布时间】:2016-07-27 20:43:35 【问题描述】:这是我在网上找到的代码,用于根据今天的日期获取本周的所有日期。如果日期是月初,例如 2016 年 9 月 1 日星期四,则该周从 星期四 开始,并在 2016 年 9 月 7 日星期三。这真的很困扰我,因为如果日期是月底; 2016 年 7 月 31 日星期日 然后它将在该周的 星期一 开始并在 星期日 结束,这正是我想要的。
这可能是重复的,但我花了一段时间查看类似的问题,但我仍然卡住了。
编辑: 有没有办法根据当前日期返回当前一周的所有日期?(如果你有时间,下周也是如此?)
func formatDate(date: NSDate) -> String
let format = "EEEE MMMM dd yyyy"
let formatter = NSDateFormatter()
formatter.dateFormat = format
return formatter.stringFromDate(date)
// =======================================================================//
// THIS WEEK DATES //
// =======================================================================//
func formattedDaysInThisWeek() -> [String]
// create calendar
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
// today's date
let today = NSDate()
let todayComponent = calendar.components([.Day, .Month, .Year], fromDate: today)
let components = calendar.components([.Weekday], fromDate: today)
// range of dates in this week
let thisWeekDateRange = calendar.rangeOfUnit(.Day, inUnit:.WeekOfMonth, forDate:today)
// date interval from today to beginning of week
let dayInterval = thisWeekDateRange.location - todayComponent.day
print(thisWeekDateRange.location)
print(todayComponent.day)
// date for beginning day of this week, ie. this week's Sunday's date
if components.weekday == 1
print("Is a sunday")
let beginningOfWeek = calendar.dateByAddingUnit(.Day, value: -6, toDate: today, options: .MatchNextTime)
var formattedDays: [String] = []
for i in 0 ..< 7
let date = calendar.dateByAddingUnit(.Day, value: i, toDate: beginningOfWeek!, options: .MatchNextTime)!
formattedDays.append(formatDate(date))
return formattedDays
else
print("Not a sunday")
let beginningOfWeek = calendar.dateByAddingUnit(.Day, value: dayInterval, toDate: today, options: .MatchNextTime)
var formattedDays: [String] = []
for i in 1 ..< thisWeekDateRange.length + 1
let date = calendar.dateByAddingUnit(.Day, value: i, toDate: beginningOfWeek!, options: .MatchNextTime)!
formattedDays.append(formatDate(date))
return formattedDays
【问题讨论】:
【参考方案1】:此解决方案检查当前日期是否为星期一。
如果是,这是一周的开始,如果否,回到过去的星期一。
它还使用formatDate
函数。
func formattedDaysInThisWeek() -> [String]
// create calendar
let calendar = Calendar(identifier: .gregorian)
// today's date
let today = Date()
let weekday = calendar.component(.weekday, from: today)
let beginningOfWeek : Date
if weekday != 2 // if today is not Monday, get back
let weekDateConponent = DateComponents(weekday: 2)
beginningOfWeek = calendar.nextDate(after: today, matching: weekDateConponent, matchingPolicy: .nextTime, direction: .backward)!
else // today is Monday
beginningOfWeek = calendar.startOfDay(for: today)
var formattedDays = [String]()
for i in 0..<7
let date = calendar.date(byAdding: .day, value: i, to: beginningOfWeek)!
formattedDays.append(formatDate(date))
return formattedDays
【讨论】:
【参考方案2】:我不知道这里是否有实际问题,但我写了一个快速的 sn-p,它总是会给你本周的星期日:
let today = NSDate()
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let todayComps = calendar.components([.Day, .Month, .Year], fromDate: today)
let dayOfWeekComps = calendar.components([.Weekday], fromDate: today)
todayComps.day -= (dayOfWeekComps.weekday - 1)
let sunday = calendar.dateFromComponents(todayComps)
【讨论】:
以上是关于如果日期是月初,则获取当前周的所有日期返回不正确的主要内容,如果未能解决你的问题,请参考以下文章