简体   繁体   English

比较两个日期以获取组件

[英]Comparing two dates for getting components

I am comparing two dates and it works perfectly when we perform the operation on the same month however when month is higher ie aug 8 and date is lower let say 2 and current date is 27 and mont is July 7 so for this comparison I am getting wrong and negative values.我正在比较两个日期,当我们在同一个月执行操作时它工作得很好但是当月份较高时,即 8 月 8 日并且日期较低时假设 2 并且当前日期是 27 并且 mont 是 7 月 7 日所以对于这个比较我得到错误和消极的价值观。

Here is my function:这是我的 function:

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))"
}

The date comparison can be performed using a Calendar function and DateComponents可以使用 Calendar function 和 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!))")

So I think your function can be shortened to所以我认为你的 function 可以缩短为

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!))"
}

Note that is ok to force unwrap date components for delta that has been selected when calling the calendar.dateComponents(_,from:, to:) function请注意,可以强制解开调用calendar.dateComponents(_,from:, to:) function 时选择的delta的日期组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM