问题描述
我尝试比较两天忽略时间.所以我用
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(来自:01-01-2016 00:05")!var date2 = dateFormatter.date(来自:01-01-2016 03:30")if let dateFromString = dateFormatter.date(from: "01-01-2016 00:05") {打印(日期从字符串)dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)让 stringFromDate = dateFormatter.string(from: dateFromString)}Calendar.current.compare(date1, to: date2!, toGranularity: .day) == .orderedSame
详情
- Xcode 11.5 (11E608c)、Swift 5.1
想法
基于使用情况dateComponents(_:from:to:)功能
解决方案
导入基础延期日期{func fullDistance(从日期:Date,resultIn 组件:Calendar.Component,日历:Calendar = .current)->诠释?{calendar.dateComponents([component], from: self, to: date).value(for: component)}func距离(从日期:Date,仅组件:Calendar.Component,日历:Calendar = .current)->诠释{let days1 = calendar.component(component, from: self)let days2 = calendar.component(组件,来自:日期)返回第 1 天 - 第 2 天}func hasSame(_ component: Calendar.Component, as date: Date) ->布尔 {距离(从:日期,仅:组件)== 0}}
完整样本
<块引用>别忘了把解决方案代码放在这里(看上面)
var dateFormatter = DateFormatter()dateFormatter.dateFormat = "dd-MM-yyyy hh:mm:ss"//dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)让smallerDate = dateFormatter.date(来自:01-01-2012 00:05:01")!让更大的日期 = dateFormatter.date(来自:03-12-2019 09:30:01")!print(smallerDate.fullDistance(from: largeDate, resultIn: .day))//可选的(2893)print(biggerDate.fullDistance(来自:smallerDate,resultIn:.day))//可选(-2893)print(smallerDate.fullDistance(from: largeDate, resultIn: .year))//可选的(7)print(biggerDate.fullDistance(from: smallDate, resultIn: .year))//可选的(7)print(smallerDate.fullDistance(from: largeDate, resultIn: .hour))//可选的(69441)print(biggerDate.fullDistance(from:smallerDate, resultIn: .hour))//可选的(-69441)print(smallerDate.distance(from: largeDate, only: .day))//-2print(biggerDate.distance(from: smallDate, only: .day))//2print(smallerDate.distance(from: largeDate, only: .year))//-7print(biggerDate.distance(from: smallDate, only: .year))//7print(smallerDate.distance(from: largeDate, only: .hour))//-9print(biggerDate.distance(from: smallDate, only: .hour))//9print(smallerDate.hasSame(.day, as: largeDate))//falseprint(biggerDate.hasSame(.second, as:smallerDate))//true
I try to compare two days ignoring time. So I use
calendar.compare(date1, to: date2, toGranularity: .day)
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
Details
- Xcode 11.5 (11E608c), Swift 5.1
Idea
base on usage dateComponents(_:from:to:) function
Solution
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
Do not forget to put here the Solution code (look above)
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:按天比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!