gpt4 book ai didi

swift - 如何快速获取本周星期一的日期

转载 作者:IT王子 更新时间:2023-10-29 05:16:11 29 4
gpt4 key购买 nike

我正在尝试获取本周星期一的日期。在我的表格 View 中,这被视为一周的第一天。我还需要获取本周的星期日。在我的 TableView 中,这被视为一周的最后一天。

当前尝试:

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
calendar.firstWeekday = 1
//attempt to changefirstday

let dateFormatter = NSDateFormatter()
let theDateFormat = NSDateFormatterStyle.ShortStyle
let theTimeFormat = NSDateFormatterStyle.ShortStyle
dateFormatter.dateStyle = theDateFormat
dateFormatter.timeStyle = theTimeFormat

let currentDateComponents = calendar.components([.YearForWeekOfYear, .WeekOfYear ], fromDate: date)
let startOfWeek = calendar.dateFromComponents(currentDateComponents)
print("startOfWeek is \(startOfWeek)")
let stringDate = dateFormatter.stringFromDate(startOfWeek!)
print("string date is \(stringDate)") //This is returning Sunday's date

最佳答案

我编写了日期扩展来获取某个工作日的日期,这就是使用 Swift 5 是多么容易,

Date.today()                                  // Oct 15, 2019 at 9:21 AM
Date.today().next(.monday) // Oct 21, 2019 at 9:21 AM
Date.today().next(.sunday) // Oct 20, 2019 at 9:21 AM


Date.today().previous(.sunday) // Oct 13, 2019 at 9:21 AM
Date.today().previous(.monday) // Oct 14, 2019 at 9:21 AM

Date.today().previous(.thursday) // Oct 10, 2019 at 9:21 AM
Date.today().next(.thursday) // Oct 17, 2019 at 9:21 AM
Date.today().previous(.thursday,
considerToday: true) // Oct 10, 2019 at 9:21 AM


Date.today().next(.monday)
.next(.sunday)
.next(.thursday) // Oct 31, 2019 at 9:21 AM

这是日期扩展,

extension Date {

static func today() -> Date {
return Date()
}

func next(_ weekday: Weekday, considerToday: Bool = false) -> Date {
return get(.next,
weekday,
considerToday: considerToday)
}

func previous(_ weekday: Weekday, considerToday: Bool = false) -> Date {
return get(.previous,
weekday,
considerToday: considerToday)
}

func get(_ direction: SearchDirection,
_ weekDay: Weekday,
considerToday consider: Bool = false) -> Date {

let dayName = weekDay.rawValue

let weekdaysName = getWeekDaysInEnglish().map { $0.lowercased() }

assert(weekdaysName.contains(dayName), "weekday symbol should be in form \(weekdaysName)")

let searchWeekdayIndex = weekdaysName.firstIndex(of: dayName)! + 1

let calendar = Calendar(identifier: .gregorian)

if consider && calendar.component(.weekday, from: self) == searchWeekdayIndex {
return self
}

var nextDateComponent = calendar.dateComponents([.hour, .minute, .second], from: self)
nextDateComponent.weekday = searchWeekdayIndex

let date = calendar.nextDate(after: self,
matching: nextDateComponent,
matchingPolicy: .nextTime,
direction: direction.calendarSearchDirection)

return date!
}

}

// MARK: Helper methods
extension Date {
func getWeekDaysInEnglish() -> [String] {
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "en_US_POSIX")
return calendar.weekdaySymbols
}

enum Weekday: String {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
}

enum SearchDirection {
case next
case previous

var calendarSearchDirection: Calendar.SearchDirection {
switch self {
case .next:
return .forward
case .previous:
return .backward
}
}
}
}

关于swift - 如何快速获取本周星期一的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397101/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com