加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 将时间戳字符串与epochal时间和时区转换为NSDate

发布时间:2020-12-14 04:51:43 所属栏目:百科 来源:网络整理
导读:我有以下格式的字符串 "/Date(573465600000-0800)/" 如何将其转换为常规NSDate对象? 解决方法 第一部分“573465600000”是自Unix时代以来的时间 以毫秒为单位,第二部分“-0800”是时区 规格. 这是对Parsing JSON (date) to Swift的略微修改 其中还包括时区
我有以下格式的字符串

"/Date(573465600000-0800)/"

如何将其转换为常规NSDate对象?

解决方法

第一部分“573465600000”是自Unix时代以来的时间
以毫秒为单位,第二部分“-0800”是时区
规格.

这是对Parsing JSON (date) to Swift的略微修改
其中还包括时区部分:

extension NSDate {
    convenience init?(jsonDate: String) {
        let prefix = "/Date("
        let suffix = ")/"
        let scanner = NSScanner(string: jsonDate)

        // Check prefix:
        if scanner.scanString(prefix,intoString: nil) {

            // Read milliseconds part:
            var milliseconds : Int64 = 0
            if scanner.scanLongLong(&milliseconds) {
                // Milliseconds to seconds:
                var timeStamp = NSTimeInterval(milliseconds)/1000.0

                // Read optional timezone part:
                var timeZoneOffset : Int = 0
                if scanner.scanInteger(&timeZoneOffset) {
                    let hours = timeZoneOffset / 100
                    let minutes = timeZoneOffset % 100
                    // Adjust timestamp according to timezone:
                    timeStamp += NSTimeInterval(3600 * hours + 60 * minutes)
                }

                // Check suffix:
                if scanner.scanString(suffix,intoString: nil) {
                    // Success! Create NSDate and return.
                    self.init(timeIntervalSince1970: timeStamp)
                    return
                }
            }
        }

        // Wrong format,return nil. (The compiler requires us to
        // do an initialization first.)
        self.init(timeIntervalSince1970: 0)
        return nil
    }
}

例:

if let theDate = NSDate(jsonDate: "/Date(573465600000-0800)/") {
    println(theDate)
} else {
    println("wrong format")
}

输出:

1988-03-04 00:00:00 +0000

更新Swift 3(Xcode 8):

extension Date {
    init?(jsonDate: String) {
        let prefix = "/Date("
        let suffix = ")/"
        let scanner = Scanner(string: jsonDate)

        // Check prefix:
        guard scanner.scanString(prefix,into: nil)  else { return nil }

        // Read milliseconds part:
        var milliseconds : Int64 = 0
        guard scanner.scanInt64(&milliseconds) else { return nil }
        // Milliseconds to seconds:
        var timeStamp = TimeInterval(milliseconds)/1000.0

        // Read optional timezone part:
        var timeZoneOffset : Int = 0
        if scanner.scanInt(&timeZoneOffset) {
            let hours = timeZoneOffset / 100
            let minutes = timeZoneOffset % 100
            // Adjust timestamp according to timezone:
            timeStamp += TimeInterval(3600 * hours + 60 * minutes)
        }

        // Check suffix:
        guard scanner.scanString(suffix,into: nil) else { return nil }

        // Success! Create NSDate and return.
        self.init(timeIntervalSince1970: timeStamp)
    }
}

例:

if let theDate = Date(jsonDate: "/Date(573465600000-0800)/") {
    print(theDate)
} else {
    print("wrong format")
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读