久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將帶有紀元時間和時區的時間戳字符串轉換為

Convert timestamp string with epochal time and timezone into NSDate(將帶有紀元時間和時區的時間戳字符串轉換為 NSDate)
本文介紹了將帶有紀元時間和時區的時間戳字符串轉換為 NSDate的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個以下格式的字符串

I have a String in following format

"/Date(573465600000-0800)/"

如何將其轉換為常規 NSDate 對象?

How do I convert this to regular NSDate object?

推薦答案

第一部分573465600000"是Unix紀元以來的時間以毫秒為單位,第二部分-0800"是時區規范.

The first part "573465600000" is the time since the Unix epoch in milliseconds, and the second part "-0800" is a time zone specification.

這里是將JSON(日期)解析為Swift的小改動這也涵蓋了時區部分:

Here is a slight modification of Parsing JSON (date) to Swift which also covers the time zone part:

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

這篇關于將帶有紀元時間和時區的時間戳字符串轉換為 NSDate的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
Make background color change during scroll(在滾動期間更改背景顏色)
How to set UIScrollView Height in Swift(如何在 Swift 中設置 UIScrollView 高度)
Disable UIPageViewController bouncing - Swift(禁用 UIPageViewController 彈跳 - Swift)
主站蜘蛛池模板: 男女午夜激情视频 | 久久一久久 | 中文字幕av一区 | 日本精品一区二区三区在线观看视频 | 精品一区二区三区在线观看国产 | 色综合久久久 | 伊人网综合在线 | 日本久久黄色 | 日日夜夜精品视频 | 亚洲国产精品久久久久 | 欧美成人第一页 | 久久国产欧美日韩精品 | 高清黄色网址 | 日韩黄 | 欧美一区成人 | 99久久久久 | 欧美一级二级三级 | 精品国产网| 久久国产精品免费一区二区三区 | 黄色三级毛片 | 2019天天干天天操 | 新av在线| 欧美精品一二三 | 91看片在线观看 | 九九热国产精品视频 | 手机在线观看 | 日本大香伊一区二区三区 | 在线免费观看成年人视频 | 日韩欧美国产精品 | 超碰在线人人 | 国产精品久久久久久久7电影 | 91在线视频观看 | 亚洲高清在线免费观看 | 在线观看国产www | 免费的av网站 | 国产精品高潮呻吟久久 | 国产精品成人在线观看 | 中文字幕一区二区三区四区五区 | 久久精品国产一区 | 本道综合精品 | 国产精品一区二区在线 |