项目需求:根据用户选择的某时间段及7天中的某几天重复执行某种操作
步骤:1、获取当前时间,并获取当前时间对应的星期
var today = Date()
let todayDay = TimeAndDateOperation.getweekDayWithDate(today)
//根据日期获取是星期几
class func getweekDayWithDate(_ date : Date) ->String{
let calensar : Calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
let comps = (calensar as NSCalendar).component(NSCalendar.Unit.weekday, from: date)
print("comps :\(comps)")
var str : String = String()
if comps == 1 {
str = "星期一"
}else if comps == 2 {
str = "星期二"
}else if comps == 3 {
str = "星期三"
}else if comps == 4 {
str = "星期四"
}else if comps == 5 {
str = "星期五"
}else if comps == 6 {
str = "星期六"
}else if comps == 7 {
str = "星期日"
}
return str
}
2、设置日期格式,并根据传入的时间段字符串startStr、endStr、当前时间转换成Date类型
let dateFormat = DateFormatter.init()
dateFormat.dateFormat = "HH:mm"
//将日期转换成字符串
let todayStr = dateFormat.string(from: today)
//转换成date类型。日期置为方法默认日期
today = dateFormat.date(from: todayStr)!
//start end 格式 "2016-05-18 9:00"
let start = dateFormat.date(from: startStr)
let end = dateFormat.date(from: endStr)
3、日期对比
1)先对比当前日期的星期是否在传入的参数dayDay即选中的星期内(这里用的方式是存储在本地,再进行字符串包含子字符串的判断,当然也可以用数组来存储,看个人喜好),如果包含进入2),否则返回false
dateDay.contains(todayDay)
2)判断开始的时间start与结束的时间end的比较的结果(以24点为界)--两种情况
(1)结束时间在24点之前--例如:start:18:00 end:23:00
ComparisonResult.orderedAscending://end未过24点
(2)结束时间在24点之后--例如:start:18:00 end:7:00
ComparisonResult.orderedDescending://end过24点
3)根据2)的结果分别判断当前时间与开始与结束时间的比较
针对2)->(1)有三种情况:
(1)开始时间<当前时间<结束时间
(today.compare(start!)==ComparisonResult.orderedDescending)&&(today.compare(end!)==ComparisonResult.orderedAscending)
(2)开始时间=当前时间<结束时间
(today.compare(start!)==ComparisonResult.orderedSame)&&(today.compare(end!)==ComparisonResult.orderedAscending)
(3)开始时间<当前时间=结束时间
(today.compare(start!)==ComparisonResult.orderedDescending)&&(today.compare(end!)==ComparisonResult.orderedSame)
针对2)->(2)有四种情况:
(1)开始时间<当前时间>结束时间----例如:start 18:00 now 23:00 end 7:00
(today.compare(start!)==ComparisonResult.orderedDescending)&&(today.compare(end!)==ComparisonResult.orderedDescending)
(2)开始时间>当前时间<结束时间----例如:start 18:00 now 2:00 end 7:00
(today.compare(start!)==ComparisonResult.orderedAscending)&&(today.compare(end!)==ComparisonResult.orderedAscending)
(3)开始时间=当前时间>结束时间----例如:start 18:00 now 18:00 end 7:00
(today.compare(start!)==ComparisonResult.orderedSame)&&(today.compare(end!)==ComparisonResult.orderedDescending)
(4)开始时间>当前时间=结束时间----例如:start 18:00 now 7:00 end 7:00
(today.compare(start!)==ComparisonResult.orderedAscending)&&(today.compare(end!)==ComparisonResult.orderedSame)
完整代码如下:
//判断当前时间是否在某个时间段内
class func judgeDateByStartAndEnd(startStr : String, endStr : String, dateDay : String)->Bool{
var today = Date()
let todayDay = TimeAndDateOperation.getweekDayWithDate(today)
let dateFormat = DateFormatter.init()
dateFormat.dateFormat = "HH:mm"
//将日期转换成字符串
let todayStr = dateFormat.string(from: today)
//转换成date类型。日期置为方法默认日期
today = dateFormat.date(from: todayStr)!
//start end 格式 "2016-05-18 9:00"
let start = dateFormat.date(from: startStr)
let end = dateFormat.date(from: endStr)
//比对日期
//1、对比是否是选中的星期中
if dateDay.contains(todayDay) {
let orderResult : ComparisonResult = (start?.compare(end!))!
switch orderResult{
case ComparisonResult.orderedAscending://end未过24点
if ((today.compare(start!)==ComparisonResult.orderedDescending)&&(today.compare(end!)==ComparisonResult.orderedAscending))||((today.compare(start!)==ComparisonResult.orderedDescending)&&(today.compare(end!)==ComparisonResult.orderedSame))||((today.compare(start!)==ComparisonResult.orderedSame)&&(today.compare(end!)==ComparisonResult.orderedAscending)) {
return true
}else{
return false
}
case ComparisonResult.orderedDescending://end过24点
if ((today.compare(start!)==ComparisonResult.orderedDescending)&&(today.compare(end!)==ComparisonResult.orderedDescending))||((today.compare(start!)==ComparisonResult.orderedAscending)&&(today.compare(end!)==ComparisonResult.orderedAscending))||((today.compare(start!)==ComparisonResult.orderedSame)&&(today.compare(end!)==ComparisonResult.orderedDescending))||((today.compare(start!)==ComparisonResult.orderedAscending)&&(today.compare(end!)==ComparisonResult.orderedSame)) {
return true
}else{
return false
}
default:
return false
}
}else{
return false
}
}
测试代码:
let jugmentResult = TimeAndDateOperation.judgeDateByStartAndEnd(startStr: "15:30", endStr: "18:40", dateDay: "星期一,星期二,星期三,星期四,星期五,星期六,星期日")
print("jugmentResult :\(jugmentResult)")