ios swift中的时间比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24316521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:58:31  来源:igfitidea点击:

Time comparisons in swift

iosswift

提问by Byron Coetsee

Question:

题:

I need to compare 2 times - the current time and a set one. If the set time is in the future, find out how many minutes remain until said future time.

我需要比较 2 次 - 当前时间和一组时间。如果设置的时间是在未来,找出距离所述未来时间还有多少分钟。

Other Info:

其他信息:

I am currently using

我目前正在使用

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
let minutes = components.minute

which I stole from another answer on SO about how to get the current time in Int format. I then split the future time into hour (Int) and minutes(Int) and compare those... But that gets odd when you go over the hour barrier.

我从关于如何以 Int 格式获取当前时间的另一个答案中窃取的。然后我将未来的时间分成小时 (Int) 和分钟 (Int) 并比较它们......但是当你超过小时障碍时,这会变得很奇怪。

回答by Duyen-Hoa

You have compare function to compare 2 NSDate to know which one is more recent. It returns NSCompareResults

您有比较功能来比较 2 个 NSDate 以了解哪个更新。它返回 NSCompareResults

enum NSComparisonResult : Int {
    case OrderedAscending
    case OrderedSame
    case OrderedDescending
}

Get distance (in seconds) from 2 NSDate, you have .timeIntervalSinceDate(). Then, you know how to convert to minutes, hours, ...

从 2 NSDate 获取距离(以秒为单位),您有 .timeIntervalSinceDate()。然后,您知道如何转换为分钟、小时、...

let date1 : NSDate = ... 
let date2 : NSDate = ...

let compareResult = date1.compare(date2)

let interval = date1.timeIntervalSinceDate(date2)

回答by Michael

just to add to @tyt_g207's answer, I found the compare method, but hadn't found NSComparisonResult.OrderedDescendingand the others. I used something like the modified below to check an expiration date against today's date

只是为了添加到@tyt_g207 的答案中,我找到了比较方法,但没有找到NSComparisonResult.OrderedDescending和其他方法。我使用类似下面修改的东西来检查今天的日期的到期日期

let date1 : NSDate = expirationDate 
let date2 : NSDate = NSDate() //initialized by default with the current date

let compareResult = date1.compare(date2)
if compareResult == NSComparisonResult.OrderedDescending {
    println("\(date1) is later than \(date2)")
}

let interval = date1.timeIntervalSinceDate(date2)

回答by Byron Coetsee

    let dateComparisionResult: NSComparisonResult = currentDate.compare("Your Date")


    if dateComparisionResult == NSComparisonResult.OrderedAscending
    {
        // Current date is smaller than end date.
    }
    else if dateComparisionResult == NSComparisonResult.OrderedDescending
    {
        // Current date is greater than end date.

    }
    else if dateComparisionResult == NSComparisonResult.OrderedSame
    {
        // Current date and end date are same.

    }

回答by Naveen Prasad R

Use timeIntervalSinceDateof date on further date and pass the earlier date as parameter, this would give the time difference

timeIntervalSinceDate在进一步日期上使用日期并将较早日期作为参数传递,这将产生时差