比较两个日期以获取组件
Comparing two dates for getting components
我正在比较两个日期,当我们在同一个月执行操作时,它工作得很好,但是当月份较高时,即 8 月 8 日和日期较低时,假设 2,当前日期是 27 日,mont 是 7 月 7 日,所以这个比较我得到了错误的负值。
这是我的函数:
func countDonwn(isStumpStarted: Bool = false)-> String? {
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second, .day], from: date as Date)
let currentDate = calendar.date(from: components)?.toLocalTime()
let userCalendar = Calendar.current
guard let endDte = (self.getDate(defaultFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")) else {return nil }
let competitionDate = calendar.dateComponents([.hour, .minute, .second, .day], from: endDte as Date)
if endDte < (date as Date) {
return ""
}
let competitionDay = userCalendar.date(from: competitionDate as DateComponents)!.toLocalTime()
let CompetitionDayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: competitionDay)
let daysLeft = CompetitionDayDifference.day
let hoursLeft = CompetitionDayDifference.hour
let minutesLeft = CompetitionDayDifference.minute
let secondLeft = CompetitionDayDifference.second
if daysLeft != 0 && !isStumpStarted {
return "\(String(describing: daysLeft ?? 0))d \(String(format: "%02d", hoursLeft ?? 0)):\(String(format: "%02d", minutesLeft ?? 0)):\(String(format: "%02d", secondLeft ?? 0))"
} else if daysLeft == 0 && hoursLeft == 0 && minutesLeft == 0 && secondLeft == 0 {
return ""
}
return "\(String(format: "%02d", hoursLeft ?? 0)):\(String(format: "%02d", minutesLeft ?? 0)):\(String(format: "%02d", secondLeft ?? 0))"
}
可以使用 Calendar 函数和 DateComponents 执行日期比较
let calendar = Calendar.current
let delta = calendar.dateComponents([.hour, .minute, .second],
from: date,
to: endDte)
print("\(String(format: "%02d", delta.hour!)):\(String(format: "%02d", delta.minute!)):\(String(format: "%02d", delta.second!))")
所以我认为你的函数可以缩短为
func countDonwn(isStumpStarted: Bool = false)-> String? {
let date = Date()
guard let endDte = (self.getDate(defaultFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")),
date.compare(endDte) == .orderedAscending else {
return nil
}
let calendar = Calendar.current
let delta = calendar.dateComponents([.hour, .minute, .second], from: fromDate, to: toDate)
return "\(String(format: "%02d", delta.hour!)):\(String(format: "%02d", delta.minute!)):\(String(format: "%02d", delta.second!))"
}
请注意,可以强制展开调用 calendar.dateComponents(_ ,from:, to:)
函数时选择的 delta
的日期组件
我正在比较两个日期,当我们在同一个月执行操作时,它工作得很好,但是当月份较高时,即 8 月 8 日和日期较低时,假设 2,当前日期是 27 日,mont 是 7 月 7 日,所以这个比较我得到了错误的负值。
这是我的函数:
func countDonwn(isStumpStarted: Bool = false)-> String? {
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second, .day], from: date as Date)
let currentDate = calendar.date(from: components)?.toLocalTime()
let userCalendar = Calendar.current
guard let endDte = (self.getDate(defaultFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")) else {return nil }
let competitionDate = calendar.dateComponents([.hour, .minute, .second, .day], from: endDte as Date)
if endDte < (date as Date) {
return ""
}
let competitionDay = userCalendar.date(from: competitionDate as DateComponents)!.toLocalTime()
let CompetitionDayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: competitionDay)
let daysLeft = CompetitionDayDifference.day
let hoursLeft = CompetitionDayDifference.hour
let minutesLeft = CompetitionDayDifference.minute
let secondLeft = CompetitionDayDifference.second
if daysLeft != 0 && !isStumpStarted {
return "\(String(describing: daysLeft ?? 0))d \(String(format: "%02d", hoursLeft ?? 0)):\(String(format: "%02d", minutesLeft ?? 0)):\(String(format: "%02d", secondLeft ?? 0))"
} else if daysLeft == 0 && hoursLeft == 0 && minutesLeft == 0 && secondLeft == 0 {
return ""
}
return "\(String(format: "%02d", hoursLeft ?? 0)):\(String(format: "%02d", minutesLeft ?? 0)):\(String(format: "%02d", secondLeft ?? 0))"
}
可以使用 Calendar 函数和 DateComponents 执行日期比较
let calendar = Calendar.current
let delta = calendar.dateComponents([.hour, .minute, .second],
from: date,
to: endDte)
print("\(String(format: "%02d", delta.hour!)):\(String(format: "%02d", delta.minute!)):\(String(format: "%02d", delta.second!))")
所以我认为你的函数可以缩短为
func countDonwn(isStumpStarted: Bool = false)-> String? {
let date = Date()
guard let endDte = (self.getDate(defaultFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")),
date.compare(endDte) == .orderedAscending else {
return nil
}
let calendar = Calendar.current
let delta = calendar.dateComponents([.hour, .minute, .second], from: fromDate, to: toDate)
return "\(String(format: "%02d", delta.hour!)):\(String(format: "%02d", delta.minute!)):\(String(format: "%02d", delta.second!))"
}
请注意,可以强制展开调用 calendar.dateComponents(_ ,from:, to:)
函数时选择的 delta
的日期组件