如何计算从时区开始经过的时间?

时间:2016-06-03 06:50:48

标签: ios swift swift2 nsdate

我从服务器获得这种格式的日期:

1 day 5 hours 10 minutes 20 seconds

现在,我想检测一下,自那个日期以来经过了多少时间?例如2016-05-27 17:33:43+0400

我该怎么办?我知道如何从时间戳计算这个,但不知道如何将其转换为时间戳。

任何人都可以帮我吗?

例如:

将此1464370423转换为var theList = new[]{-1,0,1}.Select(x=>betttime1.AddSeconds(x)).ToList()

或许还有另一种解决方案。我只想计算自那时起经过的时间

5 个答案:

答案 0 :(得分:2)

您可以使用NSDateComponents格式化程序来获取两个日期之间的相对时间。关于日期字符串格式,您需要使用xx作为时区部分。

let dateStr = "2016-05-27 17:33:43+0400"

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ssxx"
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
if let date = formatter.dateFromString(dateStr) {
    print(date)   // "2016-05-27 13:33:00 +0000\n" -4hs
    let dateComponentsFormatter = NSDateComponentsFormatter()
    dateComponentsFormatter.allowedUnits = [.Day,.Hour,.Minute,.Second]
    dateComponentsFormatter.unitsStyle = .Full
    print(dateComponentsFormatter.stringFromDate(date, toDate: NSDate()) ?? "") // 6 days, 17 hours, 51 minutes, 29 seconds
}

答案 1 :(得分:1)

给定输入字符串,您可以将其转换为日期,然后使用以下内容将其转换为NSTimeInterval当前时间。

let inputDate = "2016-05-27 17:33:43+0400"

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZZZZZ"

if let aDate = dateFormatter.dateFromString(inputDate) {

    let timeInterval = aDate.timeIntervalSinceNow

    let dateComponentsFormatter = NSDateComponentsFormatter()

    if let dateString = dateComponentsFormatter.stringFromTimeInterval(abs(timeInterval)) {

        print ("Elapsed time=\(dateString)")
    }
}

输出:

  

经过时间= 6d 17:51:52

您可以使用aDate.timeIntervalSince1970

将日期作为问题中的时间戳

答案 2 :(得分:0)

我建议这可以帮助您:How to get the current time as datetime

您可以使用服务器时间和当前时间来获取当前时间并计算差异。

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

希望它有所帮助。

答案 3 :(得分:0)

这会告诉你

  

“多少时间”

自事件发生以来。

1.7
1.9
2.7
2.9

let date = Date() let howManyTimePassed = date.timeIntervalSinceNow TimeInterval的{​​{1}},这意味着它是另一种说typealias的方式。 double的值代表秒数。

答案 4 :(得分:0)

Swift 5.1 中尝试该课程:

open class MyDateClass: NSObject {

    private var result:MyDateTime!
    
     init(dateStr: String, inputFormat: String) {
        let formatter = DateFormatter()
        formatter.dateFormat = inputFormat //"yyyy-MM-dd HH:mm:ss"
        formatter.calendar = Calendar(identifier: .gregorian)
        formatter.locale = Locale(identifier: "en_US_POSIX")
        if let date = formatter.date(from: dateStr) {
            let dateComponentsFormatter = DateComponentsFormatter()
            dateComponentsFormatter.allowedUnits = [.year,.month,.day,.hour,.minute,.second]
            dateComponentsFormatter.unitsStyle = .full
            let strFromat = dateComponentsFormatter.string(from: date, to: Date()) ?? ""
            var trimmedString = strFromat.replacingOccurrences(of: " ", with: "")
            trimmedString = trimmedString.replacingOccurrences(of: "years", with: "")
            trimmedString = trimmedString.replacingOccurrences(of: "months", with: "")
            trimmedString = trimmedString.replacingOccurrences(of: "days", with: "")
            trimmedString = trimmedString.replacingOccurrences(of: "hours", with: "")
            trimmedString = trimmedString.replacingOccurrences(of: "minutes", with: "")
            trimmedString = trimmedString.replacingOccurrences(of: "seconds", with: "")
            let arr = trimmedString.split(separator: ",")
            let result = MyDateTime(year: Int(arr[0]), month: Int(arr[1]), day: Int(arr[2]), hour: Int(arr[3]), minute: Int(arr[4]), second: Int(arr[5]))
            self.result = result
        }
    }
    
    func getDateTime() -> MyDateTime {
        return result
    }
      
}

public struct MyDateTime {
    var year:Int?
    var month:Int?
    var day:Int?
    var hour:Int?
    var minute:Int?
    var second:Int?
}

该类的工作方式如下:

let myClass = MyDateClass(dateStr: "2016-05-27 17:33:43", inputFormat: "yyyy-MM-dd HH:mm:ss")
let time = myClass.getDateTime()

现在您可以根据需要显示时间了。