我目前正在处理一些代码,这些代码将返回两个日期之间的所有日期。然后,该方法将返回2个日期,一个日期,时间设置为一天的开始,一个日期结束。这是我需要做的api调用所必需的,它会返回当天的一些数据。
然而,我遇到了夏令时引起的问题,因为时钟在26日回归,我没有得到正确的日期返回25日 - 26日。以下是我的代码
func dateTesting()
{
let day = -86400
let startDate = Date().addingTimeInterval(TimeInterval(day * 5))
let endDate = Date()
let tuples = daysBetweenDates(startDate: startDate, endDate: endDate)
print(tuples)
}
func daysBetweenDates(startDate: Date, endDate: Date) -> [(startDate: Date, endDate: Date)]
{
let calendar = Calendar.current
let components = calendar.dateComponents([.day, .month,.year], from: startDate)
let components2 = calendar.dateComponents([.day,.month,.year], from: endDate)
let daysDifferent = abs(components.day! - components2.day!)
var tupleList:[(startDate: Date, endDate: Date)] = []
let day = -86400
for index in stride(from: daysDifferent, to: 0, by: -1) {
let date = Date().addingTimeInterval(TimeInterval(day * index))
let tuple = createFetchRequestDates(date: date)
tupleList.append(tuple)
}
return tupleList
}
func createFetchRequestDates(date : Date) -> (startDate: Date, endDate: Date)
{
let calendar = Calendar.current
var components = calendar.dateComponents([.hour, .minute, .day, .month, .year], from: date)
var components2 = calendar.dateComponents([.hour, .minute, .day, .month, .year], from: date)
components.hour = 1
components.minute = 0
components2.hour = 24
components2.minute = 59
let start = calendar.date(from: components)
let end = calendar.date(from: components2)
//debugging
print(date)
print(start!)
print(end!)
print(" ")
let tuple = (startDate : start!, endDate : end!)
return tuple
}
在createFetchRequestDates代码中,当我打印出任何非夏令时日代码的代码时,它按预期工作,即
2017-03-29 09:52:37 +0000
2017-03-29 00:00:00 +0000
2017-03-29 23:59:00 +0000
然而,当在夏令时发生时在25日至26日之间调用代码时,将打印以下内容
2017-03-25 09:52:37 +0000
2017-03-25 01:00:00 +0000
2017-03-26 00:59:00 +0000
2017-03-26 09:52:37 +0000
2017-03-26 01:00:00 +0000
2017-03-26 23:59:00 +0000
有谁知道解决此问题的最佳方法?我想确保每天都能获得正确的开始和结束时间,而时钟的返回会引起一些问题。
我可以使用if语句检查特定日期是否为夏令时,然后修改这些特定日期的组件代码。但是我希望可能会采用更清洁的方法。
非常感谢任何帮助
编辑:
仅举一个问题的例子
let calendar = Calendar.current
var components = calendar.dateComponents([.day, .month,.year], from: Date())
var components2 = calendar.dateComponents([.day, .month,.year], from: Date())
components.day = 26
components.hour = 0
components.minute = 0
components2.day = 26
components2.hour = 23
components2.minute = 59
print(calendar.date(from: components)!)
print(calendar.date(from: components2)!)
打印出来
2017-03-26 00:00:00 +0000
2017-03-26 22:59:00 +0000
我希望它能在23:59:00打印出来,但不幸的是,夏令时将它打了一小时。
编辑2:
以下是更新代码的示例
func daysBetweenDates(startDate: Date, endDate: Date) -> [(startDate: Date, endDate: Date)]
{
let calendar = Calendar.current
let components = calendar.dateComponents([.day, .month,.year], from: startDate)
let components2 = calendar.dateComponents([.day,.month,.year], from: endDate)
let daysDifferent = abs(components.day! - components2.day!)
var tupleList:[(startDate: Date, endDate: Date)] = []
for index in stride(from: daysDifferent, to: 0, by: -1) {
let calendar = Calendar.current
var components = calendar.dateComponents([.hour, .minute, .day, .month, .year], from: Date())
components.day = components.day! - index
let date = calendar.date(from: components)
let tuple = createFetchRequestDates(date: date!)
tupleList.append(tuple)
}
return tupleList
}
因此,我不是减去86400秒,而是使用组件,只需减去一整天。