Convert timestamp string with epochal time and timezone into NSDate

前端 未结 4 1542
小鲜肉
小鲜肉 2020-12-30 17:35

I have a String in following format

\"/Date(573465600000-0800)/\"

How do I convert this to regular NSDate object?

相关标签:
4条回答
  • 2020-12-30 17:54
    var date:NSDate = NSDate(timeIntervalSince1970: timeInterval)
    
    0 讨论(0)
  • 2020-12-30 17:59

    let myTimeStamp = "1463689800000.0"

        let dateTimeStamp = NSDate(timeIntervalSince1970:Double(myTimeStamp)!/1000)  //UTC time
    
        let dateFormatter = NSDateFormatter()
        dateFormatter.timeZone = NSTimeZone.localTimeZone() //Edit
        dateFormatter.dateFormat = "yyyy-MM-dd"
        dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
    
    
        let strDateSelect = dateFormatter.stringFromDate(dateTimeStamp)
        print(strDateSelect) //Local time
        let dateFormatter2 = NSDateFormatter()
        dateFormatter2.timeZone = NSTimeZone.localTimeZone()
        dateFormatter2.dateFormat = "yyyy-MM-dd"
    
        let date3 = dateFormatter.dateFromString(strDateSelect)
    
        datepicker.date = date3!
    
    0 讨论(0)
  • 2020-12-30 18:01

    Swift 4

    let date = Date(timeIntervalSince1970: TimeInterval(1463689800000.0))
    
    0 讨论(0)
  • 2020-12-30 18:10

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

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

    Example:

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

    Output:

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

    Update for 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)
        }
    }
    

    Example:

    if let theDate = Date(jsonDate: "/Date(573465600000-0800)/") {
        print(theDate)
    } else {
        print("wrong format")
    }
    
    0 讨论(0)
提交回复
热议问题