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

將帶有紀(jì)元時(shí)間和時(shí)區(qū)的時(shí)間戳字符串轉(zhuǎn)換為

Convert timestamp string with epochal time and timezone into NSDate(將帶有紀(jì)元時(shí)間和時(shí)區(qū)的時(shí)間戳字符串轉(zhuǎn)換為 NSDate)
本文介紹了將帶有紀(jì)元時(shí)間和時(shí)區(qū)的時(shí)間戳字符串轉(zhuǎn)換為 NSDate的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)以下格式的字符串

I have a String in following format

"/Date(573465600000-0800)/"

如何將其轉(zhuǎn)換為常規(guī) NSDate 對(duì)象?

How do I convert this to regular NSDate object?

推薦答案

第一部分573465600000"是Unix紀(jì)元以來(lái)的時(shí)間以毫秒為單位,第二部分-0800"是時(shí)區(qū)規(guī)范.

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的小改動(dòng)這也涵蓋了時(shí)區(qū)部分:

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

<小時(shí)>

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

這篇關(guān)于將帶有紀(jì)元時(shí)間和時(shí)區(qū)的時(shí)間戳字符串轉(zhuǎn)換為 NSDate的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設(shè)置滾動(dòng)視圖內(nèi)容大小)
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
Make background color change during scroll(在滾動(dòng)期間更改背景顏色)
How to set UIScrollView Height in Swift(如何在 Swift 中設(shè)置 UIScrollView 高度)
Disable UIPageViewController bouncing - Swift(禁用 UIPageViewController 彈跳 - Swift)
主站蜘蛛池模板: 高清色| 三级免费毛片 | 毛片在线看片 | 亚洲精品一区二区网址 | 九九热精品在线 | 精品福利av导航 | 亚洲三区在线观看 | 91精品久久久久久久久 | 成人黄色在线观看 | 欧美激情精品久久久久久 | 午夜精品一区二区三区在线视频 | 亚洲国产精品一区二区第一页 | 精品日韩一区二区 | 高清国产一区二区 | 日韩精品在线一区 | 欧美日韩专区 | 国产精品视频999 | 欧美精品综合在线 | 日韩理论电影在线观看 | 欧美精品一区二区三 | 久久久久免费 | 国产专区在线 | 欧美一区二区三区精品免费 | 久久视频精品 | 成人av电影天堂 | 久久69精品久久久久久久电影好 | 中文字幕不卡 | 国产成人综合网 | 国产九九九 | 欧美一区2区三区4区公司二百 | 国产japanhdxxxx麻豆 | 在线一区二区三区 | 欧美久久久久久久久中文字幕 | 欧美精品久久 | 欧美一级做性受免费大片免费 | 伊人久麻豆社区 | a国产一区二区免费入口 | 91精品国产91久久久久久最新 | 最新国产精品精品视频 | 精品久久国产 | 91久操视频|