在家手痒就给自己找了点事做做,下面是关于怎么获取某个时间点对应时间段的一些方法,包含本周(上周,下周)开始结束日期,本月(上月,下月)开始结束日期,本年(上年,下年)开始结束日期的获取方法。内容以后会持续更新:
(封装在一个自定义的类中(DateEncapsulation))
指定日期的当前周,上周,下周 的时间段
// MARK: - week
static func getThisWeek(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let comp = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
// 获取今天是周几, 今天是几号
if let weekDay = comp.weekday, let day = comp.day {
// 计算当前日期和本周的星期一和星期天相差天数
var firstDiff: Int
var lastDiff: Int
// weekDay = 1;
if (weekDay == 1) {
firstDiff = -6;
lastDiff = 0;
} else {
firstDiff = calendar.firstWeekday - weekDay + 1
lastDiff = 8 - weekDay
}
// 在当前日期(去掉时分秒)基础上加上差的天数
var firstDayComp = calendar.dateComponents([.year, .month, .day], from: date)
firstDayComp.day = day + firstDiff
var lastDayComp = calendar.dateComponents([.year, .month, .day], from: date)
lastDayComp.day = day + lastDiff
if let firstDayOfWeek = calendar.date(from: firstDayComp), let lastDayOfWeek = calendar.date(from: lastDayComp) {
let firstDay = dateFormatter.string(from: firstDayOfWeek)
let lastDay = dateFormatter.string(from: lastDayOfWeek)
return (firstDate: firstDay, lastDate: lastDay)
}
}
return nil
}
static func getLastWeek(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let comp = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
// 获取今天是周几, 今天是几号
if let weekDay = comp.weekday, let day = comp.day {
// 计算当前日期和本周的星期一和星期天相差天数
var firstDiff: Int
var lastDiff: Int
// weekDay = 1;
if (weekDay == 1) {
firstDiff = -6;
lastDiff = 0;
} else {
firstDiff = calendar.firstWeekday - weekDay + 1
lastDiff = 8 - weekDay
}
// 上周,减七天
firstDiff -= 7
lastDiff -= 7
// 在当前日期(去掉时分秒)基础上加上差的天数
var firstDayComp = calendar.dateComponents([.year, .month, .day], from: date)
firstDayComp.day = day + firstDiff
var lastDayComp = calendar.dateComponents([.year, .month, .day], from: date)
lastDayComp.day = day + lastDiff
if let firstDayOfWeek = calendar.date(from: firstDayComp), let lastDayOfWeek = calendar.date(from: lastDayComp) {
let firstDay = dateFormatter.string(from: firstDayOfWeek)
let lastDay = dateFormatter.string(from: lastDayOfWeek)
return (firstDate: firstDay, lastDate: lastDay)
}
}
return nil
}
static func getNextWeek(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let comp = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
// 获取今天是周几, 今天是几号
if let weekDay = comp.weekday, let day = comp.day {
// 计算当前日期和本周的星期一和星期天相差天数
var firstDiff: Int
var lastDiff: Int
// weekDay = 1;
if (weekDay == 1) {
firstDiff = -6;
lastDiff = 0;
} else {
firstDiff = calendar.firstWeekday - weekDay + 1
lastDiff = 8 - weekDay
}
// 下周,加七天
firstDiff += 7
lastDiff += 7
// 在当前日期(去掉时分秒)基础上加上差的天数
var firstDayComp = calendar.dateComponents([.year, .month, .day], from: date)
firstDayComp.day = day + firstDiff
var lastDayComp = calendar.dateComponents([.year, .month, .day], from: date)
lastDayComp.day = day + lastDiff
if let firstDayOfWeek = calendar.date(from: firstDayComp), let lastDayOfWeek = calendar.date(from: lastDayComp) {
let firstDay = dateFormatter.string(from: firstDayOfWeek)
let lastDay = dateFormatter.string(from: lastDayOfWeek)
return (firstDate: firstDay, lastDate: lastDay)
}
}
return nil
}
指定日期的当前月,上月,下月 的时间段
// MARK: - month
static func getThisMonth(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let currentComponents = calendar.dateComponents([.year, .month], from: date)
if let startDate = calendar.date(from: currentComponents) {
// 当月第一天
let startDayString = dateFormatter.string(from: startDate)
var diffComponents = DateComponents()
// 比当月一日多一个月少一天即为当月最后一日
diffComponents.month = 1
diffComponents.day = -1
if let endDate = calendar.date(byAdding: diffComponents, to: startDate) {
let endDayString = dateFormatter.string(from: endDate)
return (firstDate: startDayString, lastDate: endDayString)
}
}
return nil
}
static func getLastMonth(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// 当前传入日期 转成 Date 格式
let calendar = Calendar.current
var currentComponents = calendar.dateComponents([.year, .month, .day], from: date)
// 设置为 1,转成 Date 后为当前传入日期月份的第一天
currentComponents.day = 1
if let startOfMonth = calendar.date(from: currentComponents) {
var diffComponents = DateComponents()
// 比当前月一日少一个月即为上月一日
diffComponents.month = -1
// 当月第一天
if let startDate = calendar.date(byAdding: diffComponents, to: startOfMonth) {
let startDayString = dateFormatter.string(from: startDate)
// 比当前月少一天即为上月最后一日
diffComponents.month = 0
diffComponents.day = -1
if let endDate = calendar.date(byAdding: diffComponents, to: startOfMonth) {
let endDayString = dateFormatter.string(from: endDate)
return (firstDate: startDayString, lastDate: endDayString)
}
}
}
return nil
}
static func getNextMonth(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
// 当前传入日期 转成 DateComponents 格式
var currentComponents = calendar.dateComponents([.year, .month, .day], from: date)
// 设置为 1,转成 Date 后为当前传入日期月份的第一天
currentComponents.day = 1
if let startOfMonth = calendar.date(from: currentComponents) {
// 处理时间差
var diffComponents = DateComponents()
// 添加一个月的时间差
diffComponents.month = 1
// 下个月第一天
if let startDate = calendar.date(byAdding: diffComponents, to: startOfMonth) {
let startDayString = dateFormatter.string(from: startDate)
// 和当前月第一天相差两个月少一天即为下个月最后一天时间
diffComponents.month = 2
diffComponents.day = -1
if let endDate = calendar.date(byAdding: diffComponents, to: startOfMonth) {
let endDayString = dateFormatter.string(from: endDate)
return (firstDate: startDayString, lastDate: endDayString)
}
}
}
return nil
}
指定日期的当前年,上一年,下一年 的时间段
// MARK: - year
static func getThisYear(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let currentComponents = calendar.dateComponents([.year], from: date)
if let startDate = calendar.date(from: currentComponents) {
// 当年第一天
let startDayString = dateFormatter.string(from: startDate)
var diffComponents = DateComponents()
// 比当年一月一号多一年少一天即为当年最后一日
diffComponents.year = 1
diffComponents.day = -1
if let endDate = calendar.date(byAdding: diffComponents, to: startDate) {
let endDayString = dateFormatter.string(from: endDate)
return (firstDate: startDayString, lastDate: endDayString)
}
}
return nil
}
static func getLastYear(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let currentComponents = calendar.dateComponents([.year], from: date)
if let startDate = calendar.date(from: currentComponents) {
// 当年第一天
// let startOfday = dateFormatter.string(from: startOfMonth)
var diffComponents = DateComponents()
diffComponents.year = -1
if let startOfLastYear = calendar.date(byAdding: diffComponents, to: startDate) {
let startDayString = dateFormatter.string(from: startOfLastYear)
// 比当年一月一号多一年少一天即为当年最后一日
diffComponents.year = 0
diffComponents.day = -1
if let endDate = calendar.date(byAdding: diffComponents, to: startDate) {
let endDayString = dateFormatter.string(from: endDate)
return (firstDate: startDayString, lastDate: endDayString)
}
}
}
return nil
}
static func getNextYear(_ date: Date) -> (firstDate: String, lastDate: String)? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = Calendar.current
let currentComponents = calendar.dateComponents([.year], from: date)
if let startOfMonth = calendar.date(from: currentComponents) {
// 当年第一天
// let startOfday = dateFormatter.string(from: startOfMonth)
var diffComponents = DateComponents()
diffComponents.year = 1
if let startDate = calendar.date(byAdding: diffComponents, to: startOfMonth) {
let startDayString = dateFormatter.string(from: startDate)
// 比当年一月一号多一年少一天即为当年最后一日
diffComponents.year = 2
diffComponents.day = -1
if let endDate = calendar.date(byAdding: diffComponents, to: startOfMonth) {
let endDayString = dateFormatter.string(from: endDate)
return (firstDate: startDayString, lastDate: endDayString)
}
}
}
return nil
}
为了方便获取,又做了一些集中处理,加了两个枚举类型
// 周/月/年
enum CalendarFormMode {
case week
case month
case year
}
// 上周/月/年, 本周/月/年, 下周/月/年
enum CalendarSequence {
case this(CalendarFormMode)
case last(CalendarFormMode)
case next(CalendarFormMode)
}
// 通过输入 enum 获取 对应时间的 时间段
class func dateSpanFromDate(_ date: Date, mode: DateEncapsulation.CalendarSequence) -> (firstDate: String, lastDate: String)? {
switch mode {
case .this(.week):
return DateEncapsulation.getThisWeek(date)
case .this(.month):
return DateEncapsulation.getThisMonth(date)
case .this(.year):
return DateEncapsulation.getThisYear(date)
case .last(.week):
return DateEncapsulation.getLastWeek(date)
case .last(.month):
return DateEncapsulation.getLastMonth(date)
case .last(.year):
return DateEncapsulation.getLastYear(date)
case .next(.week):
return DateEncapsulation.getNextWeek(date)
case .next(.month):
return DateEncapsulation.getNextMonth(date)
case .next(.year):
return DateEncapsulation.getNextYear(date)
}
}
exp:
DateEncapsulation.dateSpanFromDate(Date(), mode: .this(.week))