Swift:按天数比较日期

12

我试图比较两天,而忽略时间。因此我使用

calendar.compare(date1, to: date2, toGranularity: .day)
但是,当将字符串日期转换为Date类型时,它会被转换为UTC。因此,它将从1.1.2016 0:05“移动”到31.12.2015 11:05 pm。在按天比较时,这仅包括剩余的小时数。在转换之前,它是24小时。有没有什么方法可以轻松处理这个问题?
此外: 代码:
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
var date1 : Date = dateFormatter.date(from:  "01-01-2016 00:05")!
var date2 = dateFormatter.date(from:  "01-01-2016 03:30")

if let dateFromString = dateFormatter.date(from:  "01-01-2016 00:05") {
    print(dateFromString)
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
    let stringFromDate = dateFormatter.string(from: dateFromString)
}


Calendar.current.compare(date1, to: date2!, toGranularity: .day) == .orderedSame

以上代码比较给定日期是否在同一天或不同的日子,根据日历。请提供一个自包含的可重现示例,包括预期输出和实际输出。 - Martin R
我添加了代码,但更加困惑。在进行更多测试之前,我必须再做一些测试! :-) - Peter71
你并没有非常清楚地表达实际的问题。问题是它转换为UTC格式导致了问题,还是在转换时丢失了小时信息,或者是在转换后不再是24小时格式?尝试重新描述并明确你所遇到的问题,这将使你更快地得到答案。 - torinpitchers
1个回答

33

详情

  • Xcode 11.5(11E608c),Swift 5.1

想法

基于使用dateComponents(_:from:to:)函数

解决方案

import Foundation

extension Date {

    func fullDistance(from date: Date, resultIn component: Calendar.Component, calendar: Calendar = .current) -> Int? {
        calendar.dateComponents([component], from: self, to: date).value(for: component)
    }

    func distance(from date: Date, only component: Calendar.Component, calendar: Calendar = .current) -> Int {
        let days1 = calendar.component(component, from: self)
        let days2 = calendar.component(component, from: date)
        return days1 - days2
    }

    func hasSame(_ component: Calendar.Component, as date: Date) -> Bool {
        distance(from: date, only: component) == 0
    }
}

完整示例

不要忘记在此处放置解决方案代码(请参见上面)

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm:ss"
//dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

let smallerDate = dateFormatter.date(from: "01-01-2012 00:05:01")!
let biggerDate  = dateFormatter.date(from: "03-12-2019 09:30:01")!

print(smallerDate.fullDistance(from: biggerDate, resultIn: .day))       // Optional(2893)
print(biggerDate.fullDistance(from: smallerDate, resultIn: .day))       // Optional(-2893)
print(smallerDate.fullDistance(from: biggerDate, resultIn: .year))      // Optional(7)
print(biggerDate.fullDistance(from: smallerDate, resultIn: .year))      // Optional(7)
print(smallerDate.fullDistance(from: biggerDate, resultIn: .hour))      // Optional(69441)
print(biggerDate.fullDistance(from: smallerDate, resultIn: .hour))      // Optional(-69441)

print(smallerDate.distance(from: biggerDate, only: .day))               // -2
print(biggerDate.distance(from: smallerDate, only: .day))               // 2
print(smallerDate.distance(from: biggerDate, only: .year))              // -7
print(biggerDate.distance(from: smallerDate, only: .year))              // 7
print(smallerDate.distance(from: biggerDate, only: .hour))              // -9
print(biggerDate.distance(from: smallerDate, only: .hour))              // 9

print(smallerDate.hasSame(.day, as: biggerDate))                        // false
print(biggerDate.hasSame(.second, as: smallerDate))                     // true

请注意,dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) 将根据设备的 GMT 更改日期,这可能导致不同的日期。 - RodolfoAntonici
1
@RodolfoAntonici,没错,因为我想比较具有相同时区的“干净”日期。 - Vasily Bodnarchuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接