let Now = Date() // todayvar matchingComponents = DateComponents()matchingComponents.weekday = 2 // Mondaylet comingMonday = Calendar.current.nextDate(after: Now,matching: matchingComponents,matchingPolicy:.nextTime)
在这里,是一个查找下一个星期一的简单方法.如果今天是星期一,则以下函数今天返回,否则最接近的下一个星期一.请注意,它使用en_POSIX_US,以便可以匹配日期.当语言环境为en_POSIX_US时,工作日符号变为,
["Sunday","Monday","Tuesday","Wednesday","Thursday","FrIDay","Saturday"]
而且,这是如何使用这些日子,
func findNext(_ day: String,afterDate date: Date) -> Date? { var calendar = Calendar.current calendar.locale = Locale(IDentifIEr: "en_US_POSIX") let weekDaySymbols = calendar.weekdaySymbols let indexOfDay = weekDaySymbols.index(of: day) assert(indexOfDay != nil,"day passed should be one of \(weekDaySymbols),invalID day: \(day)") let weekDay = indexOfDay! + 1 let components = calendar.component(.weekday,from: date) if components == weekDay { return date } var matchingComponents = DateComponents() matchingComponents.weekday = weekDay // Monday let nextDay = calendar.nextDate(after: date,matchingPolicy:.nextTime) return nextDay!}let nextMonday = findNext("Monday",afterDate: Date())let mondayAfterThat = findNext("Monday",afterDate: nextMonday!)let thursday = findNext("Thursday",afterDate: mondayAfterThat!)总结
以上是内存溢出为你收集整理的可可 – 查找任何日期下一个最接近的特定日期的NSDate全部内容,希望文章能够帮你解决可可 – 查找任何日期下一个最接近的特定日期的NSDate所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)