How to convert date type \ / Date (1440156888750-0700) \ / to what Swift can handle?

so in my application I need to deal with a date like \/Date(1440156888750-0700)\/ , I think the first part means seconds since January 1, 1970, and the second part means time zone. I don’t know how to process such data and display it the way we all understand in Xcode 7 using Swift 2?

+3
source share
2 answers

(The previous version of this answer was actually incorrect; it did not correctly handle the time zone.)

According to autonomous JSON serialization :

DateTime values ​​are displayed as JSON strings in the form "/ Date (700000 +0500) /", where the first number (700000 in the above example) is the number of milliseconds in the GMT time zone, normal (without daylight saving time)) midnight January 1, 1970. The number may be negative to represent earlier times. The part that consists of "+0500" in the example is optional and indicates that the time is local, that is, it must be converted to a local time zone during deserialization. If it is absent, the time is deserialized as Utc. The actual number (in this example, β€œ0500”) and its sign (+ or -) are ignored.

and use JSON.NET to parse the date in json format. Date (epochTime-offset)

... In this [screw format] [1], the timestamp part is still based solely on UTC. Offset is additional information. This does not change the time stamp. You can specify another offset or completely omit it, and it will remain at that moment in time.

the first number in \/Date(1440156888750-0700)\/ is the number of milliseconds that have passed since the "era" of January 1, 1970 GMT, and the -0700 part of the time zone -0700 simply ignored.

Here is a Swift 5 extension method for Date that validates the string using a regular expression (accepting as \/Date(...)\/ and /Date(...)/ , with /Date(...)/ or without time zone) and converts the specified number of milliseconds to Date :

 extension Date { init?(jsonDate: String) { let pattern = #"\\?/Date\((\d+)([+-]\d{4})?\)\\?/"# let regex = try! NSRegularExpression(pattern: pattern) guard let match = regex.firstMatch(in: jsonDate, range: NSRange(jsonDate.startIndex..., in: jsonDate)) else { return nil } // Extract milliseconds: let dateString = jsonDate[Range(match.range(at: 1), in: jsonDate)!] // Convert to UNIX timestamp in seconds: let timeStamp = Double(dateString)! / 1000.0 // Create Date from timestamp: self.init(timeIntervalSince1970: timeStamp) } } 

Example:

 let jsonDate = "\\/Date(1440156888750-0700)\\/" print("JSON Date:", jsonDate) if let theDate = Date(jsonDate: jsonDate) { print("Date:", theDate) } else { print("wrong format") } 

Output:

  JSON Date: \ / Date (1440156888750-0700) \ /
 Date: 2015-08-21 11:34:48 +0000 

(Versions for Swift 3 and Swift 4 can be found in the editing history.)

+6
source

After several experiments, I came up with the following solution:

 let dx = "/Date(1440156888750-0700)/" let timestamp = (dx as NSString).substringWithRange(NSRange(location: 6,length: 13)) let timezone = (dx as NSString).substringWithRange(NSRange(location: 19,length: 5)) let dateIntermediate = NSDate(timeIntervalSince1970: Double(timestamp)! / 1000) let outp = NSDateFormatter() outp.dateFormat = "dd.MM.yyyy hh:mm::ssSSS" outp.timeZone = NSTimeZone(forSecondsFromGMT: 0) let input = outp.stringFromDate(dateIntermediate) + " " + timezone let inp = NSDateFormatter() inp.dateFormat = "dd.MM.yyyy hh:mm::ssSSS Z" let finalDate = inp.dateFromString(input) print(finalDate) 

Let me explain:

  • we extract the millisecond timestamp and timezone from the original row
  • we create a date from a timestamp to divide it into its various components.
  • we display this date in a more standard way (and not as a timestamp) and add the previously allocated time zone to this line
  • Then we read this line and parse the date from it again

Note
As @ Phoen1xUK noted, a timestamp can have a different length than 13 digits. You can handle this situation by splitting /Date( )/ and then splitting the string up to - (or + ).

+2
source

Source: https://habr.com/ru/post/980881/


All Articles