Swift:逐日比较日期 [英] Swift: Compare date by days

查看:85
本文介绍了Swift:逐日比较日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试比较两天忽略时间。所以我用

I try to compare two days ignoring time. So I use

calendar.compare(date1, to: date2, toGranularity: .day)

但将字符串日期转换为Date类型时,它将转换为UTC。因此它将从1.1.2016 0:05移动到31.12.2015 11:05 pm。按日比较时,这仅包括剩余时间。转换之前是24小时。有什么想法可以解决这个问题吗?

BUT when transfering the string date to Date type, it is transformed to UTC. So it will be "moved" from 1.1.2016 0:05 to 31.12.2015 11:05 pm. When comparing daywise, this includes only the remaining hour. Before conversion it was 24 hours. Any idea to handle this issue without much effort?

另外:
代码:

Additionally: The code:

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


推荐答案

详细信息




  • 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
        }
    }
    



    完整样本



    Full Sample


    别忘了在此处输入 Solution 代码(看一下



    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
    

    这篇关于Swift:逐日比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆